1、错误描述
C:UsersAdministrator.USER-0GUONPPBHK>cd G:HBuilderXWorkVueserver
C:UsersAdministrator.USER-0GUONPPBHK>g:
G:HBuilderXWorkVueserver>node index
G:HBuilderXWorkVue
ode_modulesexpresslib
outerindex.js:458
throw new TypeError('Router.use() requires a middleware function but got a
' + gettype(fn))
^
TypeError: Router.use() requires a middleware function but got a Object
at Function.use (G:HBuilderXWorkVue
ode_modulesexpresslib
outerindex.
js:458:13)
at Function.<anonymous> (G:HBuilderXWorkVue
ode_modulesexpresslibappli
cation.js:220:21)
at Array.forEach (<anonymous>)
at Function.use (G:HBuilderXWorkVue
ode_modulesexpresslibapplication.j
s:217:7)
at Object.<anonymous> (G:HBuilderXWorkVueserverindex.js:11:5)
at Module._compile (internal/modules/cjs/loader.js:721:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
at Function.Module._load (internal/modules/cjs/loader.js:552:3)
G:HBuilderXWorkVueserver>
2、错误原因
在定义访问数据库,并进行数据操作,没有使用module.exports
3、解决办法
const express = require('express')
const mysql = require('mysql')
const dbConfig = require('./db')
const sqlMap = require('./sqlMap')
var router = express.Router();
var conn = mysql.createConnection(dbConfig.mysql);
conn.connect();
var jsonWrite = function(res,req){
if(typeof req === 'undefined'){
res.json({
code: '1',
msg: '操作失败'
});
}else{
res.json(req);
}
}
router.post('/queryData',(res,req) => {
var sql = sqlMap.stu.select();
conn.query(sql, function(err,result){
if(err){
console.log(err);
}
if(result){
jsonWrite(res,result);
}
})
});
module.exports = router;