1.创建npm文件
npm init -y
2.安装express依赖
npm i express
3.编写js代码
1 const express = require('express') 2 3 const router = express.Router() 4 5 router.get('/msg',(req,res)=>{ 6 res.send('hello wolrd') 7 }) 8 9 let app = express() 10 11 app.use(router) 12 13 app.listen(8080,()=>{ 14 console.log('listen on port 8080') 15 })
package.json的内容如下:
{ "name": "express_test", "version": "1.0.0", "description": "", "main": "app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "express": "^4.17.1" } }
4.启动express服务
node app.js
5.测试效果
http://localhost:8080/msg