server/db/index.js
var mysql = require('mysql'); var conn = mysql.createConnection({ host : 'localhost', user : 'root', password : 'rootroot', database : 'test' }); conn.connect(); module.exports = conn;
routes/index.js
var express = require('express'); var router = express.Router(); var conn = require('../db'); //引入db /* GET home page. */ router.get('/getBannerData', function(req, res, next) {//首页swiper接口 conn.query('select * from banner', function (error, results, fields) { if (error) throw error; res.json({ results }) }); }); router.get('/getTabData', function(req, res, next) {//首页swiper下方tab接口 conn.query('select * from tabnav', function (error, results, fields) { if (error) throw error; res.json({ results }) }); }); module.exports = router;