NodeJS ๋ฐ ExpressJS - Hello World ๐จ๐ปโ๐ป
๋ด์ฉ์ ํ ์ด๋ธ
์๊ฐ
With NodeJS, you can now write backend code with JavaScript. While, for some people, it might not be too difficult to learn a new language to write server-side code, I think that being able to write both frontend and backend with the same language makes learning full stack developer easier (maybe ๐). So what is NodeJS? - NodeJS is a JavaScript runtime environment that comes with many tools to help you write server-side code using JS. You can learn more about it with nodejs .ExpressJS๋ ์ฌ์ฉํ๊ธฐ ์ฌ์ด NodeJS ํ๋ ์์ํฌ์ ๋๋ค. ๋ค์ํ ๊ฒฝ๋ก(๊ฒฝ๋ก/URL)์ ๋ํ ๋ค์ํ HTTP ์์ฒญ(GET, POST, PUT ๋ฑ)์ ๋ํ ํธ๋ค๋ฌ ํจ์๋ฅผ ์์ฑํ ์ ์์ต๋๋ค. ๋ํ ํ ํ๋ฆฟ์ ์ฌ์ฉํ์ฌ ์ ์ ํ์ผ ๋ฐ ๋์ ์ผ๋ก ์์ฑ๋ ์ฝํ ์ธ ๋ฅผ ์ ๊ณตํ๋ ๋ฐ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์์ธํ ๋ด์ฉ์ tutorialpoint ๋ฐ express์์ ํ์ธํ ์ ์์ต๋๋ค.
์ ์ ์กฐ๊ฑด
You should:
- Know some JavaScript code
- Have NodeJS installed
- Have VSCode installed (this is the editor I use, but you can use others you are comfortable with)
npm
์ ํจ๊ป ์ ๊ณต๋ฉ๋๋ค. ExpressJS.์๋ฒ ์ค์
After you have installed NodeJS, you can create a new folder (call anything you want) and open it up in VSCode, and within VSCode you can open the terminal and initialize a node project with:
npm init -y
This will start a new NodeJS project and create a package.json
file with populated information. You can then run:
npm install express
This will add ExpressJS to the dependencies list in the package.json
file and install it along with other node built in modules (you will see the node_modules
folder appear).
Next, you will need to create a JavaScript file to start coding the "Hello world" app, you can call it app.js or index.js (or whatever).
์ฝ๋ ํฌ๋ก ์๋ ์ฑ
To begin, let's import ExpressJS using require
, as express is a function, you will need to call it and store it in a variable typically called app
. We can then start the server by telling app
(a.k.a express) to listen on a specific port.
const express = require("express"); // import express
const app = express(); // initiate express
const PORT = 8000;
app.listen(PORT, () => console.log("Server is running on http://localhost:" + PORT));
The second argument to the .listen
method lets us define a callback function so we know when the server is up and running.
We can start the server by going to the terminal and run this command (*note: my js file is called index.js, so you will need to use whatever you named your js file):
node index.js
๋ค์์ผ๋ก ํ ํ์ด์ง ๋๋ ๋ฃจํธ URL("/")์ ๋ํ GET ๋์ ์ ์ค์ ํ๊ณ "Hello world"๋ก ์๋ตํฉ๋๋ค. ์ฌ๊ธฐ์๋
.get()
๋ฅผ ์ฌ์ฉํฉ๋๋ค. ์ด๋ GET ์์ฒญ์ ์๋ตํ๋ ๋ฐฉ๋ฒ์ ์๋ฒ์ ์๋ ค์ฃผ๋ Express ๋ฉ์๋์
๋๋ค. ์ฒซ ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ ์คํํ๋ ค๋ ๊ฒฝ๋ก(URL/์๋ํฌ์ธํธ)๋ฅผ ์๋ ค์ค๋๋ค. ์ด ์์์๋ ๋ฃจํธ URL("/")์ ์ํ๊ณ ๋ ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ ์์ฒญ ๋ฐ ์๋ต ๊ฐ์ฒด. "Hello world"๋ฅผ ์ธ์ํ๋ ค๋ฉด res
๊ฐ์ฒด์ .send
๋ฉ์์ง๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.app.get("/", function(req, res) {
res.send("Hello world!");
});
์ง์! ํด๋์ด! ์ง๊ธ ๋ธ๋ผ์ฐ์ ์์ ํ์ด์ง๋ฅผ ๋ฐฉ๋ฌธํ๋ฉด ์๋ฒ๊ฐ "Hello world!"๋ผ๋ ๋ฉ์์ง๋ก GET ์์ฒญ์ ์๋ตํ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค. (๋๋ ์ฌ์ฉํ ๋ฉ์์ง). ์๋ฌด ๊ฒ๋ ํ์๋์ง ์์ผ๋ฉด Ctr + C๋ฅผ ๋๋ฌ ์๋ฒ๋ฅผ ๋ค์ ์์ํ๊ณ
node index.js
๋ฅผ ๋ค์ ์คํํ์ญ์์ค.๋จ๊ณ๋ฅผ ๋ฐ๋๋ค๋ฉด ๋ชจ๋ ๊ฒ์ด ์ ๋๋ก ์๋ํ๊ณ ExpressJS ๋ฐ NodeJS๋ฅผ ์ฌ์ฉํ์ฌ "Hello world"์ฑ์ ์ฝ๋ฉํ์ ๊ฒ์ ๋๋ค. ์ฌ๊ธฐreplit์์ ๋ด ์๋ฅผ ๋ณผ ์ ์์ต๋๋ค.
์ ์ฒด ํ๋ก์ ํธ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
const express = require("express"); // import express
const app = express(); // initiate express
const PORT = 8000;
app.get("/", function(req, res) {
res.send("Hello world!");
});
app.listen(PORT, () => console.log("Server is running on http://localhost:" + PORT));
์์ฝ
NodeJS๋ ์๋ฒ ์ธก์์ JS ์ฝ๋๋ฅผ ์์ฑํ ์ ์๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ JavaScript ๋ฐํ์ ํ๊ฒฝ์ ๋๋ค. ๋ง์ ์ ์ฉํ ๋๊ตฌ๊ฐ ๋ด์ฅ๋์ด ์์ต๋๋ค. ExpressJS๋ HTTP ์์ฒญ ๋ฑ์ ์ฒ๋ฆฌํ๋ ๋ฐ ๋์์ด ๋๋ NodeJS ํ๋ ์์ํฌ์ ๋๋ค. ์ด ๋ ๊ฐ์ง๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ๋จํ "Hello World"์ฑ์ ๋ง๋ค ์ ์์ต๋๋ค.
์ฝ์ด์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค. ์๊ฒฌ์ด ์์ผ์๋ฉด ๋๊ธ์ ๋จ๊ฒจ์ฃผ์๊ฑฐ๋ Twitter(์ผ)๋ก ์ ์๊ฒ DM์ ๋ณด๋ด์ฃผ์ธ์.
Reference
์ด ๋ฌธ์ ์ ๊ดํ์ฌ(NodeJS ๋ฐ ExpressJS - Hello World ๐จ๐ปโ๐ป), ์ฐ๋ฆฌ๋ ์ด๊ณณ์์ ๋ ๋ง์ ์๋ฃ๋ฅผ ๋ฐ๊ฒฌํ๊ณ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ๋ณด์๋ค https://dev.to/justtanwa/nodejs-and-expressjs-hello-world-3eb3ํ ์คํธ๋ฅผ ์์ ๋กญ๊ฒ ๊ณต์ ํ๊ฑฐ๋ ๋ณต์ฌํ ์ ์์ต๋๋ค.ํ์ง๋ง ์ด ๋ฌธ์์ URL์ ์ฐธ์กฐ URL๋ก ๋จ๊ฒจ ๋์ญ์์ค.
์ฐ์ํ ๊ฐ๋ฐ์ ์ฝํ ์ธ ๋ฐ๊ฒฌ์ ์ ๋ (Collection and Share based on the CC Protocol.)
์ข์ ์นํ์ด์ง ์ฆ๊ฒจ์ฐพ๊ธฐ
๊ฐ๋ฐ์ ์ฐ์ ์ฌ์ดํธ ์์ง
๊ฐ๋ฐ์๊ฐ ์์์ผ ํ ํ์ ์ฌ์ดํธ 100์ ์ถ์ฒ ์ฐ๋ฆฌ๋ ๋น์ ์ ์ํด 100๊ฐ์ ์์ฃผ ์ฌ์ฉํ๋ ๊ฐ๋ฐ์ ํ์ต ์ฌ์ดํธ๋ฅผ ์ ๋ฆฌํ์ต๋๋ค