通过express,开启服务器:
写一个最简单的接口:
下载:cnpm i experss -S
访问:http://localhost:8000/user/login
const express = require('express')
const app = express()
app.get('/user/login',(req,res)=>{
console.log('你好')
res.send('成功')
})
app.listen(8000,()=>{
console.log('server')
})
get请求验证登录:
const express = require('express')
const app = express()
app.get('/user/login',(req,res)=>{
let {us,ps} = req.query
//处理数据
if(us=== 'likang'&&ps==1994456){
res.send({err:0,msg:'密码正确'})
}else{
res.send({err:-1,msg:'密码错误'})
}
res.send('成功')
})
app.listen(8000,()=>{
console.log('server')
})
post请求:body-parser
const express = require('express')
const app = express()
const bodypaser = require('body-parser')
app.use(bodypaser.urlencoded({extended : false}))
app.get('/user/login',(req,res)=>{
let {us,ps} = req.query
//处理数据
if(us=== 'likang'&&ps==1994456){
res.send({err:0,msg:'密码正确'})
}else{
res.send({err:-1,msg:'密码错误'})
}
res.send('成功')
})
//模仿注册
app.post('/user/reg',(req,res) =>{
console.log(req.body)
let {us,ps} = req.body
if(us==123&&ps==123){
res.send({err:0,msg:'ok'})
}else{
res.send({err:-1,msg:'no'})
}
})
app.listen(8000,()=>{
console.log('server')
})
网友评论