node.js 자체는 server가 아니지만 node.js의 framework를 통하여 http server를 만들 수 있다.
먼저 프로젝트 폴더를 생성한다.
$ mkdir nodejs // 프로젝트 폴더 생성
$ cd nodejs
$ npm init // npm을 통해 project initialization
$ npm i express // express 설치
npm init
을 실행하면 name, description, author 등 정보를 입력하라고 나오는데 다 enter를 쳐서 넘어가도 정상적으로 init 된다.
index.js라는 파일을 만들어 아래 코드를 작성한다.
const express = require('express')
const app = express()
app.get('/', (req, res) => {
res.send('Hello World')
})
app.listen(3000, () => {
console.log("Express server has started on port 3000")
})
저장 후, 터미널에서 node index.js
를 실행한다.
이제 브라우저에서 http://localhost:3000
또는 http://127.0.0.1:3000
으로 접근하면 화면이 뜨는 것을 확인 할 수 있다.