express 초기세팅
초기 세팅
1. yarn init -y
2. touch app.js touch index.html
3. yarn add -D nodemon
4. yarn add express
package.json
{
"name": "express",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "nodemon app"
},
"license": "MIT",
"dependencies": {
"express": "^4.18.2"
},
"devDependencies": {
"nodemon": "^2.0.22"
}
}
index.html
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>익스프레스 서버</title>
</head>
<body>
<h1>익스프레스</h1>
<p>배워봅시다.</p>
</body>
</html>
app.js
const express = require('express');
const path = require('path');
const app = express();
app.set('port', process.env.PORT || 3000); // app에 변수를 심어놓음
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.post('/', (req, res) => {
res.send('hello express');
});
app.get('/about', (req, res) => {
res.send('hello about');
});
app.listen(app.get('port'), () => {
console.log('express server start!!');
});
$ yarn run start
[nodemon] restarting due to changes...
[nodemon] starting `node app.js`
express server start!!
Last updated