前言
我是歌谣 微信公众号关注前端小歌谣一起学习前端知识 今天继续给大家讲解获取express获取静态资源的讲解
案列
const express = require("express")
const app = express()
const IndexRouter=require("./router/indexRouter")
//配置解析
app.use(express.static("public"))
app.use(express.urlencoded({extended:false}))
app.use(express.json())
//应用
app.use(function(req,res,next){
console.log("验证token")
next()
})
app.use(function(req,res,next){
console.log("验证token")
next()
})
app.use("/",IndexRouter)
app.use((req,res)=>{
res.status(404).send("丢了")
})
app.listen(3000, () => {
console.log("server start")
})
运行结果
案例
login.html
我是登录
用户名:
密码:
var oLogin=document.querySelector("#login")
var oLoginPost=document.querySelector("#loginpost")
var oUsername=document.querySelector("#username")
var oPassword=document.querySelector("#password")
oLogin.onclick=()=>{
console.log(oUsername.value,oPassword.value)
fetch(`/login?username=${oUsername.value}&password=${oPassword.value}`)
.then(res=>res.text()).then(res=>{
console.log(res)
})
}
oLoginPost.onclick=()=>{
console.log(oUsername.value,oPassword.value)
fetch(`/lo服务器托管网gin`,{
method:"POST",
body:JSON.stringify({
username:oUsername.value,
password:oPassword.value
}),
headers:{
"Content-Type":"application/json"
}
}).then(res=>res.json()).then(res=>{
console.log(res,"res")
if(res["ok"]=="1"){
location.href="/index.html"
}else{
console.log("用户名密码不匹配")
}
})
}
indexRoute.js
const express = require("express")
const router = express.Router(服务器托管网)
router.get("/login",(req,res)=>{
res.send("get-success")
})
router.post("/login",(req,res)=>{
console.log(req.body)
const {username,password}=req.body
if(username=="geyao"&&password=="123456"){
res.send({ok:"1"})
}else{
res.send({ok:"0"})
}
})
module.exports=router
index01.js
const express = require("express")
const app = express()
const IndexRouter=require("./router/indexRouter")
//配置解析
app.use(express.static("public"))
app.use(express.urlencoded({extended:false}))
app.use(express.json())
//应用
app.use(function(req,res,next){
console.log("验证token")
next()
})
app.use(function(req,res,next){
console.log("验证token")
next()
})
app.use("/",IndexRouter)
app.use((req,res)=>{
res.status(404).send("丢了")
})
app.listen(3000, () => {
console.log("server start")
})
运行结果
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net