1.新建项目文件并且创建入口文件server.js
2.初始化
npm init
3.安装express
npm install express
4.在入口文件(server.js)中写
const express = require(‘express‘) const app = express() app.get("/",(req,res) => { res.send("Hello world!"); }) const port = process.env.PORT || 5000 //端口 app.listen(port,() => { console.log(‘Server runing on port $(port)‘) })
5.运行node
node server.js
6.当你每次更改代码时都需要重新运行node server.js这时就不方便,你可以安装 nodemon
npm install nodemon -g 运行 nodemon server.js 实现自动更新 (之前我们用node server.js)
7.修改package配置
{ "name": "app", "version": "1.0.0", "description": "restful api", "main": "server.js", "scripts": { "start":"node server.js", "server":"nodemon server.js" }, "author": "Baibu", "license": "ISC", "dependencies": { "express": "^4.17.1" } }
原文地址:https://www.cnblogs.com/sansuixs/p/12536419.html
时间: 2024-11-08 14:02:47