-
06/09 node.js게임 웹 프로그래밍/node.js 2021. 6. 9. 14:23
Express - Node.js web application framework
Fast, unopinionated, minimalist web framework for Node.js $ npm install express --save
expressjs.com
글로벌로 설치된 모듈 확인
npm list -g
postman다운로드
Postman | The Collaboration Platform for API Development
Postman makes API development easy. Our platform offers the tools to simplify each step of the API building process and streamlines collaboration so you can create better APIs faster.
www.postman.com
GPGS (Google play Game Service) //유니티
FB (Facebook) //유니티
Naver //웹방식
Kakao //웹방식
서버코드 수정시 자동 재실행 모듈
npm i nodemon -g
npm init -y
npm i express
Express에서 정적 파일 제공
아래의 코드로 정적 자산이 포함된 디폴트 디렉토리 설정
app.use(express.static('public'));
app.js
const express = require('express'); const app = express(); const port = 3000; app.use(express.static(__dirname + '/public')); //app.use(express.static('public')); app.get('/', (req, res) => { res.send("Hello world!"); }); app.post('/', (req, res) => { res.send('post방식으로 전달받은 요청을 처리하고 응답'); }) app.listen(port, ()=> { //서버가 시작될 때 실행되는 콜백 console.log(`${port}번 포트에서 서버 시작`); })
post형식으로 json데이터를 리퀘스트하여 서버에 저장후 get 리퀘스트로 데이터 취득
app.js
const express = require("express"); const userRouter = require("./routes/user"); const app = express(); const port = 3000; app.use(express.json()); app.use("/users", userRouter); app.listen(port, () => { //서버가 시작될때 실행되는 콜백 console.log(`${port}번 포트에서 서버 시작`); });
user.js
const express = require("express"); const { v4: uuidv4 } = require("uuid"); const router = express.Router(); let users = []; router.post("/", (req, res, next) => { const uuid = uuidv4(); let user = req.body; user["id"] = uuid; users.push(user); console.log(users); res.send("회원등록완료"); }); router.get("/:id", (req, res, next) => { console.log(req.params.id); console.log(users); let user = users.find(x => x.id === req.params.id); console.log("--------->" + user); let status = -1; if (user === undefined) { status = 5000; //사용자를 찾을수 없음 } else { status = 200; } let result = { status, user, }; let json = JSON.stringify(result); res.end(json); }); module.exports = router;
'게임 웹 프로그래밍 > node.js' 카테고리의 다른 글
06/11 node.js 리퀘스트의 params, query확인 (0) 2021.06.11 06/10 node.js express를 사용한 rest api 연습 (0) 2021.06.10 06/08 node.js (0) 2021.06.08 06/04 node.js (0) 2021.06.04 06/03 node.js 서버 (0) 2021.06.03