node.js 서버 만들기

소영·2022년 8월 7일
0

npm init (초기화 시켜주는 역할) ==> package.json 파일 생성됨
npm i express
npm i cors
npm i nodemon -D (개발자 모드에서 nodemon 설치)
npm i dotenv ==> .env 파일 만들고 PORT=3500 작성 (띄어쓰기 X)

server.js

const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;

const cors = require('cors');
app.use(cors());
// 데이터 가져올 때 생기는 문제를 해결할 수 있음

const path = require('path');
app.use(express.static(path.join(__dirname, 'views')));
app.use(express.static(path.join(__dirname, 'public')));

app.get('/', (req, res) => {
	res.sendFile(path.join(__dirname, 'views'), 'index.html');
})

app.listen(PORT, () => {
	console.log(`server running on PORT ${PORT}`);
})

서버 실행할 때는 npx nodemon server

0개의 댓글