一、創建package.json文件
npm init -y
二、創建index.js文件
三、安裝插件
npm install express
npm install cors
四、在index.js引入express
const express = require('express');
const cors = require('cors');
const app = express()
app.use(cors()) // 解決在前端調接口時出現的跨域問題
app.get("/",(req,res)=>{
res.send("hello word")
})
app.post("/submit", (req, res) => {
res.send({
msg: "hello word"
})
})
const PORT = 8080
app.listen(PORT, () => {
console.log("服務器正在運行http://localhost:" + PORT)
});
接口請求測試可在postman中進行測試
post請求
get請求
五、啟動nodejs環境
在package.json中配置啟動路徑
"scripts": {
"dev": "node index.js", //配置的啟動路徑 啟動命令:npm run dev
"test": "echo \"Error: no test specified\" && exit 1"
},