1.初始化package.json
npm init --yes
2.模块安装
npm install express body-parser request querystring --save
3.创建index.js文件
4.安装nodemon用于检测代码改变
npm install nodemon -g
5.index页面搭建服务器
const express=require('express')
const bodyParser=require('body-parser') //用于接收前端传来的数据
const request=require('request') //在后端请求接口
const querystring=require('querystring')
const app=express();//实例化app用于监听
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
app.get("/",(req,res)=>{
res.send("hello world")
})
const port=process.env.PORT||5000;
app.listen(port,()=>{
console.log(`server is running on port ${port}`)
})
6.ctrl+` 运行nodemon,在localhost:5000页面显示hello word,此时后端服务器已搭建完成