我正在整理节点服务器。整理的此阶段包括创建Routes文件夹,并将特定端点的特定文件放入其中。示例如下。我的问题是我们如何跨节点文件结构共享数据库?
server.js
const express = require('express');
const app = express();
const sqlite3 = require('sqlite3')
let db = new sqlite3.Database('./db.sqlite');
const userData = require('./Routes/clickUserProfileRequest.js')
app.use('/requestParticularUser', userData)
clickUserProfileRequest.js
var express = require('express');
var router = express.Router();
router.post('/', function(req, res, next) {
let currentSpecifiedUser = req.user
db.get('SELECT * FROM wholeScoresTable WHERE UserID = ?',[currentSpecifiedUser], function(err, row) {
if (err) {
console.log(err)
res.send({success:false, message:"Error", currentUser:req.user, differentUser : false, score : false})
} else if (!row) {
res.send({success:false, message : "Profile not found", currentUser:req.user, differentUser : false, score : false})
} else if (row) {
res.send({success:true, differentUser:false, message : "Loaded "+currentSpecifiedUser +"'s profile!", currentUser:req.user, score : row})
}
})
})
module.exports = router
终端
ReferenceError: db is not defined