nodejs写接口

版权声明:版权所有 休休漠漠转载请注明! https://blog.csdn.net/xiuxiumomo/article/details/82188878

db部分公共

var mysql = require('mysql');
var dbMsg = {
    host     : 'localhost',
    user     : 'root',
    password : '123456',
    database : 'app_pro'
}


var connection = mysql.createConnection(dbMsg);
    connection.connect();
module.exports = connection;

app.js

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
//获取数据库连接对象
var connection = require('./mysql/db');

//处理post字段请求
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//处理跨域请求
app.all("*", function(req, res, next) {
    res.header("Access-Control-Allow-Credentials", true);
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});


app.get('/',function(req,res){
    res.send('请求home成功');
})

//用户登录
app.post('/user/login', (req,res) => {
    var name = req.body.username;
    var passwd = req.body.password;
    var userStr = `select * from user where username="${name}" and password="${passwd}"`;
    connection.query(userStr,function(err,result){
        if(err){
            throw err;
        }else{
            res.send(result)
        }
    })
})
//用户注册


const port =  3001;
app.listen(port, () => {
    console.log('Express server listening on port ' + port);
});

猜你喜欢

转载自blog.csdn.net/xiuxiumomo/article/details/82188878