https://dongkyu.tistory.com/58
Node.js 노드 설치하기
1. Node.js 사용이유 서버를 사용하는 이유는 다음과 같다..자바스크립트를 동일하게 사용해서 서버단 로직을 처리할 수 있다는 게 가장 큰 장점! 새로운 언어를 습득하지 않고도 자바
dongkyu.tistory.com
지난 게시글에 이어 노드로 RestApi를 만들어 보겠다.
라이브러리 사용
npm | Home
Bring the best of open source to you, your team, and your company Relied upon by more than 17 million developers worldwide, npm is committed to making JavaScript development elegant, productive, and safe. The free npm Registry has become the center of Java
www.npmjs.com
https://www.npmjs.com/package/express
express
Fast, unopinionated, minimalist web framework. Latest version: 4.19.2, last published: 2 months ago. Start using express in your project by running `npm i express`. There are 81864 other projects in the npm registry using express.
www.npmjs.com
모든것을 내가 개발 하는 것은 아니다.
이미 만들어진 라이브러리를 설치해 잘 사용하는 것이 중요하다.
express 설치하기
npm install express
npm을 이용하여 express를 설치한다.
RestApi 만들기
const express = require("express");
const app = express();
const users = [{ name: "srlee", age: 30 }]
app.use(express.json())
app.get("/user", function (req, res) {
return res.send({ users: users })
})
app.post("/user", function (req, res) {
users.push({ name: "srlee", age: 30 })
return res.send({ sucess: true })
})
app.post("/user2", function (req, res) {
console.log(req.body)
users.push({ name: req.body.name, age: req.body.age })
return res.send({ sucess: true })
})
app.post("/user3", function (req, res) {
console.log(req.body)
users.push({ name: req.body.name, age: req.body.age })
return res.send({ users: users })
})
app.listen(3000, function () {
console.log("server listening on port 3000")
})
위와같이 만들면
localhost:3000/user
와 같이 사용 할 수 있다.
'Node.js' 카테고리의 다른 글
Node.js 노드 설치하기 (0) | 2024.05.10 |
---|