본문 바로가기
프로그래밍/javascript

node.js를 이용한 서버 만들기

by freeelifee 2021. 10. 8.
728x90

node.js란

서버 사이드 자바스크립트 실행환경을 의미함


1. node.js 다운로드해서 설치
https://nodejs.org/ko/

 

Node.js

Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.

nodejs.org

 

2. 윈도우에서 cmd 혹은 PowerShell 실행하고, node -v 명령으로 버전을 확인함으로서 node.js가 제대로 설치되어 있는지 확인이 가능함


3. 에디터 설치 (Visual Studio Code나 Atom 에디터를 선택해서 설치)

Visual Studio Code 설치
https://code.visualstudio.com/

 

Visual Studio Code - Code Editing. Redefined

Visual Studio Code is a code editor redefined and optimized for building and debugging modern web and cloud applications.  Visual Studio Code is free and available on your favorite platform - Linux, macOS, and Windows.

code.visualstudio.com

Atom 에디터 설치

https://atom.io/

 

A hackable text editor for the 21st Century

At GitHub, we’re building the text editor we’ve always wanted: hackable to the core, but approachable on the first day without ever touching a config file. We can’t wait to see what you build with it.

atom.io

 

4. 작업폴더를 생성하고 오픈

 

5. new terminal 실행

 

6. npm init
PS D:\vscode_workspace> npm init

This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (vscode_workspace)
version: (1.0.0)
description:
entry point: (index.js) server.js
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to D:\vscode_workspace\package.json:

{
"name": "vscode_workspace",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}


Is this OK? (yes) y


7. express라이브러리 설치 : npm install express

PS D:\vscode_workspace> npm install express
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN vscode_workspace@1.0.0 No description
npm WARN vscode_workspace@1.0.0 No repository field.

+ express@4.17.1
added 50 packages from 37 contributors and audited 50 packages in 2.715s
found 0 vulnerabilities


8. server.js 파일 생성

const express = require('express');
const app = express();

app.listen("8080", () => { 
    console.log("start server [port:8080]"); 
});

app.get("/getList", (req, res) => {
    console.log("request getList");
	res.send("hello");
});

 

9. 터미널에서 node server.js 명령으로 서버를 실행함

 

10. 웹브라우저에서 http://localhost:8080/getList 실행하면 "hello" 응답이 오는 것을 확인할 수 있음

728x90

'프로그래밍 > javascript' 카테고리의 다른 글

postman Pre-request script  (0) 2022.10.24
Vue.js 환경설정  (0) 2021.12.09
타임리프 (Thymeleaf) 정리 (2)  (0) 2021.10.07
타임리프 (Thymeleaf) 정리 (1)  (0) 2021.10.06
JSON (JavaScript Object Notation) 정리  (0) 2021.10.06