Node.js - (1) express js 세팅

Apeachicetea·2021년 11월 29일
0

Node.js

목록 보기
1/5

package.json 만들기

npm init으로 package.json에 대한 기본설정을 할 수 있다.

 kimdaeyoon  ~  cd Desktop
 kimdaeyoon  ~/Desktop  cd boiler-plate
 kimdaeyoon  ~/Desktop/boiler-plate  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: (boiler-plate)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author: Viktor
license: (ISC)
About to write to /Users/kimdaeyoon/Desktop/boiler-plate/package.json:

{
  "name": "boiler-plate",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Viktor",
  "license": "ISC"
}


Is this OK? (yes)

index.js(백엔드 시작점)

index.js 파일 생성하기

express 라이브러리 설치

npm install express

index.js에서 기본적인 express js앱 만들기

express공식문서에서 자세한 사용법을 확인할 수 있다.

const express = require('express')
//express 모듈을 가져옴
const app = express()
//새로운 express 앱을 만듬
const port = 5000
//포트는 자유롭게 사용 가능

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

npm run start 설정하기

package.json에서 다음과 작성


  "scripts": {
    "start": "node index.js",
profile
웹 프론트엔드 개발자

0개의 댓글