使用第三方模块
1.新建一个文件夹,文件夹的名字非中文,名字也不要和模块名字一样.
2.进到文件夹里面去,命令运行:npm init -y
这个命令可以理解成是初始化
3.下载模块, 去npm官网搜索模块,用他的说明来下
4.使用模块,去模块的官网,或者模块说明中来使用.
//导入express 模块
const express = require('express')
//创建一个服务器
const app = express()
// 设置返回给用户看的内容
app.get('/', function (req, res) {//如果是用内置模块http创建的服务器,返回的内容用res.end()响应.//现在我们这里用的是express模块创建的服务器,那用res. send()响应res.send('Hello World')
})
//启动服务器
app.listen(4399,()=>{console.log('服务器开启了');
})
//导入express 模块
const express = require('express')
//创建一个服务器
const app = express()app. use(express.static( 'web'))
// 设置返回给用户看的内容
// app.get('/', function (req, res) {
// res.send('Hello World')
// })
//启动服务器
app.listen(4399,()=>{console.log('服务器开启了');
})
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><style>body{background-color: red;/*background-color: green;*//*background-color: blue;*/}</style>
</head>
<body>
1页面
</body>
</html>
模块和模块化的定义,
//导入express 模块
const express = require('express')
//创建一个服务器
const app = express()
// 写接口
app.post('/sb', (req, res)=> {res.send('111')
})
//启动服务器
app.listen(4399,()=>{console.log('服务器开启了');
})
接口:得到一条随机笑话
接口地址: /joke
请求方式: get
参数:无
返回:一条笑话
//导入express 模块
const express = require('express')
//创建一个服务器
const app = express()
// 写接口
app.get('/joke', (req, res)=> {//准备n条笑话(实际开发的时候笑话们肯定是从数据库或者是其他的数据源获取到的.let arr = ['111','222','333']let index = Math .floor (Math. random()*3)//0 1 2//返回笑话res.send(arr[index])
})
//启动服务器
app.listen(4399,()=>{console.log('服务器开启了');
})
nodejs内置模块有哪些、接口:用户登录
请求地址: /login
请求方式:post
请求参数:
- username password
- 登录账号/用户名 用户密码
返回值:登录成功/登录失败
如果在输入命令时卡了或者下载错了,可以使用命令"npm cache clean -f"
//导入express 模块
const express = require('express')
const bodyParser = require('body -parser')//创建一个服务器
const app = express()// parse application/ x-www- form- urlencoded
app. use(bodyParser.urlencoded({ extended: false}))// 写接口
app.post('/login', (req, res)=> {//接收用户传递过来的用户名和密码//由于是post方式传递过来的参数,所以用req.query这种方式拿不到.// console .log(req) ;// console.log(req.query);//要想获取到通过post传递过来的参数,就要使用第三方模块:body-parser// 就用reg. body来获取参数。console.log(reg.body);//处理if(req.body.username == 'admin' && req.body.password == '888888'){res .send({code:200,msg:'登陆成功'})}else {res.send({code:400,msg:'账号密码不正确'})}res.send('111')
})
//启动服务器
app.listen(4399,()=>{console.log('服务器开启了');
})
接口:查询英雄外号 / 根据英雄名返回英雄外号
接口地址: /getNickName
请求方式: get
请求参数: heroName英雄名(提莫/盖伦/李青… )
返回值:英雄外号
//导入express 模块
const express = require('express')
//创建一个服务器
const app = express()
// 设置返回给用户看的内容
app.get('/getNickName', (req, res)=> {// 要接收前端传递过来的参数(英雄名)console . log(req. query)//处理let heroNickName ='';switch (req. query.heroName) {case '提莫':heroNickName = '迅捷斥候'break;case '提莫':heroNickName = '迅捷斥候'break;case '提莫':heroNickName = '迅捷斥候'break;default:heroNickName = '没有该英雄'break;}res.send('Hello World')
})
//启动服务器
app.listen(4399,()=>{console.log('服务器开启了');
})
功能模块化。
接口:返回一个食物
接口地址: /food
请求方式: get
请求参数:无
返回值: json
//导入express 模块
const express = require('express')
//创建一个服务器
const app = express()
// 设置返回给用户看的内容
app.get('/food', (req, res)=> {res.send({foodName: '红烧肉',price: 50,description: '好吃好吃好想吃'
})
})
//启动服务器
app.listen(4399,()=>{console.log('服务器开启了');
})
1.npm
2.npm指向淘宝镜像
node.js后端,3.cnpm
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态