NodeJS ๋ฐ ExpressJS - Hello World ๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป

9721 ๋‹จ์–ด beginnerstutorialjavascriptwebdev
์•ˆ๋…•ํ•˜์„ธ์š” ์—ฌ๋Ÿฌ๋ถ„ ๐Ÿ‘‹ ์ €๋Š” ์ตœ๊ทผ์— freeCodeCamp "๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ ๋ฐ API"๋ฅผ ์™„๋ฃŒํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ด€์‹ฌ์ด ์žˆ์œผ์‹œ๋ฉด ์—ฌ๊ธฐFCC์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‚˜๋Š” ๊ฝค ๋งŽ์€ ๊ฒƒ์„ ๋ฐฐ์› ๊ณ  ์ด๊ฒƒ์ด NodeJS๋ฅผ ์‚ฌ์šฉํ•œ ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์— ๋Œ€ํ•œ ํ›Œ๋ฅญํ•œ ์ž…๋ฌธ์„œ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ €๋Š” Express์™€ NodeJS๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ณ ์ „์ ์ธ "Hello World"์•ฑ์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์„ ๋ชจ๋‘ ๋ณด์—ฌ๋“œ๋ฆฌ์ง€ ์•Š์„ ์ด์œ ๊ฐ€ ์—†๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.

๋‚ด์šฉ์˜ ํ…Œ์ด๋ธ”


  • Introduction
  • Pre-requisites
  • Server set up
  • 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)
    If you don't have NodeJS yet, you can download it here at nodejs ๋‹ค์–‘ํ•œ ํŒจํ‚ค์ง€์™€ ์ข…์†์„ฑ์„ ์„ค์น˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ์ž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
    
    Right now, you won't be able to do much because we have not set up any routes (end-points), but you can still view and check if the server is up and running. You should see the "Server is running on http://localhost:8000 "์ฝ˜์†”์—์„œ "Cannot GET/"๋ฉ”์‹œ์ง€๊ฐ€ ๋ธŒ๋ผ์šฐ์ €์—์„œ ํ•ด๋‹น ํŽ˜์ด์ง€๋ฅผ ๋ฐฉ๋ฌธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    ๋‹ค์Œ์œผ๋กœ ํ™ˆ ํŽ˜์ด์ง€ ๋˜๋Š” ๋ฃจํŠธ 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์„ ๋ณด๋‚ด์ฃผ์„ธ์š”.

    ์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ