익스프레스(express):
npm에서 서버를 제작하는 과정에서의 불편을 해소하고 편의기능을 추가한 웹서버 프레임 워크
// package.json을 가장 먼저 생성
{
"name": "Capstone2023",
"version": "0.0.1",
"description": "make a server",
"main": "app.js",
"scripts": {
"start": "nodemon app" //필수!!!, app.js를 nodemon모듈로 서버를 자동 재시작
},
"author": "JiminOh",
"license": "MIT",
$ npm i express
$ npm i -D nodemon
const express = require('express');
const app = express();
app.set('port', process.env.PORT||3000); //서버가 실행될 포트 지정
app.get('/', (req, res) =>{
res.send('Hello, Express');
});
app.listen(app.get('port')),() => {
console.log(app.get('port'),'빈 포트에서 대기중');
});
<html>
<head>
<meta charset="UTF-8" />
<title>익스프레스 서버</title>
</head>
<body>
<h1>심화 캡스톤 졸업작품</h1>
<p>unity3D와 nodejs(벡엔드)의 연계<p>
</body>
</html>
const exrpess = require('express');
const path = require('path');
const app = express();
app.set('port', process.env.PORT || 3000);
app.get('/', (req,res) => {
res.sendFile(path.join(_dirname, '/index.html'));
});
app.listen(app.get('port'),() =>{
console.log(app.get('port'), '빈 포트에서 대기 중')
})