yarn : 速度相对较快
npm : 镜像源地址不在国内,下载安装速度慢
cnpm : 不使用它的命令,而是使用 cnpm 的镜像地址
npm install -g cnpm --registry=https://registry.npm.taobao.org
1. 准备工作
安装nodeJS : https://nodejs.org/zh-cn/
安装yarn : https://yarn.bootcss.com/
在项目目录下安装 express , 命令 yarn add express
将当前文件夹变成一个符合npm规范包 命令 yarn init
包名 question name 规范 : 不使用数字开头及不包含大写字母
/**** server.js ****/
// 一个简单的后端路由
// 1.引入express
const express = require('express');
// 2.创建一个 app 实例对象
const app = express();
// 4.配置路由 request 请求 response 响应,回应
app.get('/test_get',(request,response)=>{
response.send('hello_test_get');
})
// 3.绑定监听 端口号:四位数,不要使用1,2,3开头的端口号
// 进行回调
app.listen(8080,(err)=>{
// 函数体
if(!err) console.log('测试ajax请求的服务器开启成功了!');
})
在终端输入命令 node server.js
返回 : 测试ajax请求的服务器开启成功了!
2. 测试服务器(能处理 get 请求,能响应 /test_get 路由地址)
在浏览器中输入localhost:8080/test_get
(本地服务器) ' localhost :' + '端口号' + '路由地址'
修改请求相应内容后需重启服务器才能使浏览器端内容改变
延迟重启 nodemon
全局安装 nodemon : npm install -g nodemon
or npm install --save-dev nodemon
自动重启服务器 终端命令 nodemon server.js
作者:墨染蓮華