从节点日志文件中抛出以下错误。模块是否在服务器上放置不同位置?
这是我的server.js文件。Server running at http://127.0.0.1:8081/ module.js:471 throw err; './routes/FtseData' at Function.Module._resolveFilename (module.js:469:15) at Function.Module._load (module.js:417:25) at Module.require (module.js:497:17) at require (internal/module.js:20:19) at Object. (/var/app/current/routes.js:7:20) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) module.js:471 throw err; ^
Error: Cannot find module './routes/FtseData' at Function.Module._resolveFilename (module.js:469:15) at Function.Module._load (module.js:417:25) at Module.require (module.js:497:17) at require (internal/module.js:20:19) at Object. (/var/app/current/routes.js:7:20) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) module.js:471 throw err; ^
var express = require('express');
var basicAuth = require('express-basic-auth')
var bodyParser = require('body-parser')
var cors = require('cors');
var app = express();
app.use(cors());
app.get('/', function (req, res) {
// res.send('Hello World');
res.sendFile(__dirname + '/views/index.html');
})
var routes = require('./routes');
app.use('/api', routes);
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("App listening at http://%s:%s", host, port)
})
routes.js
var express = require('express');
var basicAuth = require('express-basic-auth')
var app = express();
var router = express.Router();
//var profCtrl = require('./routes/profileController');
var ftseDataCtrl = require('./routes/FtseData');
var ftseDivCtrl = require('./routes/FtseDiv');
//routes not needing auth
router.route('/FtseDivGet').get(ftseDivCtrl.get);
//routes needing auth
router.use(basicAuth({
users: { 'admin': 'supersecret' }
}))
//router.route('/FtseDataScrape').get(ftseDataCtrl.scrape);
router.route('/FtseDivScrape').get(ftseDivCtrl.scrape);
//http://localhost:8081/api/profile
module.exports = router;
package.json
{
"name": "StockTracker",
"version": "1.0.0",
"description": "Stock-Tracker",
"dependencies": {
"express": "*",
"express-basic-auth": "*",
"request": "*",
"cheerio": "*",
"body-parser": "*",
"cheerio-tableparser": "*",
"cors": "*",
"csv-load-sync": "*"
},
"scripts": {
"start": "node server.js"
}
}
FTseData
在routes
文件夹中,且routes.js
也在同一文件夹中,则使用require('./FtseData')
而不是require('./routes/FtseData')
。 - Santosh