其他分享
首页 > 其他分享> > express框架

express框架

作者:互联网

   

 

let express=require('express');
let urll=require('url');
let mysql=require('mysql');
let connection =mysql.createConnection({
    host:'127.0.0.1',
    user:'root',
    password:'root',
    database:'study',
});
connection.connect();
let app=express();
app.use(express.static('node_modules'));
app.set('view engine','ejs');
app.set('views','./views');
app.get('/index',(req,res)=>{
    let str='select * from student';
    connection.query(str,(error,results)=>{
        console.log(1);
        if(!error){
            console.log(2);
            res.render('index',{data:results});
            res.end();
        }
    })
})
//删除数据
app.get('/del',(req,res)=>{
    let str='delete from student where id=?';
    let url=req.url;
    let ur=urll.parse(url,true).query.id;
    let sql=mysql.format(str,ur);
    connection.query(sql,(error,results)=>{
        if(!error){
            console.log(results);
            res.redirect('/index')
            res.end();
        }    
    })
})
app.listen(8090);

 

标签:框架,url,res,app,express,let,ejs
来源: https://www.cnblogs.com/Hollow-Z/p/16503475.html