node常用

const express=require('express');
const static=require('express-static');
const mysql=require('mysql');
var multerUtil = require('./multerUtil'); //图片上传模块
// 删除图片
const fs = require('fs');

let db=mysql.createConnection({host: 'localhost', user: 'root', password: 'root', database: 'yipintang'});

let server=express();

// 允许跨域
server.all('', function(req, res, next) {
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("X-Powered-By",' 3.2.1')
res.header("Content-Type", "application/json;charset=utf-8");
next();
});

//监听端口
server.listen(7600);
server.use('/multerUtil',multerUtil);
server.use(static('www'));

server.get('/parent', (req, res)=>{
db.query(SELECT * FROM parent_db, (err, data)=>{
if(err){
res.send({ok: false, msg: '数据库错了'});
res.end();
}else{
if(data.length>0){
res.send(data);
res.end();
}else{
res.send({ok: false, msg: '有误'});
res.end();
}
}
});
});

你可能感兴趣的:(node常用)