本地搭建服务器代码

在terminal    
1 cd  文件夹  
2  node  jS 的文件夹名字
 3  npm init 
4 F:\java  script\nodeprc>npm install express@4 -s

5F:\java  script\nodeprc>npm install morgan -s
6F:\java  script\nodeprc>npm install body-parser -s
7F:\java  script\nodeprc>npm install mysql -s




const myexpress=require("express");
const logger=require("morgan")
const bodyParser=require("body-parser")
const router = require("./routers/pagerouters")
const myindex =myexpress();
myindex.use(logger("dev"))// 日志
myindex.use(bodyParser.urlencoded({extended:false}))//配置post的body解析
myindex.use(bodyParser.json());
myindex.use(myexpress.static(__dirname+"/src"));
myindex.use(router)
//探测雷达
myindex.listen(8888,()=>{
    console.log("服务器启动")
})



。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
const express=require("express")
const router= express.Router();
const usecontroller=require("../controller/pageCtrl")
router.post("/login.do",usecontroller.userlogin)
module.exports=router

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。


module.exports={
    userlogin(a,b){
        console.log("路由使用成功")
        let userName= a.body.userName
        let pwds=a.body.pwd
        console.log(userName,pwds)
        let db=mysqlModule.createConnection({
            host:"localhost",
            port:"3306",
            user:"root",
            password:"root",
            database:"myuse"

        })  //打开数据库
        db.connect();//连接数据库
        // db.query (sql语句,前台参数, 回调函数)
        db.query("SELECT uname FROM ming WHERE uname=? AND upad=?",[userName,pwds],function (err,data) {
            if(data.length>0){
                b.send(data[0].uname+"登录成功")
            }else{
                b.send("登录失败")
            }
        })
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值