Localhost express routing不起作用

时间:2018-02-04 09:27:59

标签: node.js

我在localhost上有一个节点项目,可以工作几个月。 今天我的Mac上有更新。 现在,当我尝试通过浏览器 on localhost 访问节点项目时,我只是得到一个空白屏幕。 但在我的VPS 上,同一个项目按预期工作。

在终端上,有日志表示节点已启动并正在运行,但是当我尝试访问某些api时,我发现了一个错误。

这是主要的app.js文件:

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var mongojs = require('mongojs');
var db = mongojs("mongodb://user:pass@ip:port/dbname", []);
var ObjectId = require('mongodb').ObjectID;
var CronJob = require('cron').CronJob;
var cors = require('cors');
var request = require('request');
var fcm = require('./routes/fcm');
var Excel = require('exceljs');
var fs = require('fs');
var path = require('path');
var rateLimit = require('express-rate-limit');

var limiter = new rateLimit({
  windowMs: 10*60*1000, // 10 minutes
  max: 100, // limit each IP to 100 requests per windowMs (11 is on load and more 100 later on)
  delayMs: 0, // disable delaying - full speed until the max limit is reached
  message: "נחסמת ל-10 דקות בעקבות שימוש לא סביר בשרת - נסה במועד מאוחר יותר"
});

//  apply to all requests
app.use(limiter);

app.use(cors());

var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.use(express.static(__dirname + '/src'));

// just for check if a simple call working:
app.get("/checkthis", function(req, res, next) {
  res.send("Yes");
});

require('./routes/sockets')(app, io, mongojs, db, ObjectId, CronJob, request, fcm);
require('./routes/userServer')(app, io, mongojs, db, ObjectId, CronJob, request, fcm, rateLimit);
require('./routes/ridesServer')(app, io, mongojs, db, ObjectId, CronJob, request);
require('./routes/offersServer')(app, io, mongojs, db, ObjectId, CronJob, request, rateLimit);
require('./routes/notificationsServer')(app, io, mongojs, db, ObjectId, CronJob, request);
require('./routes/scopeServer')(app, io, mongojs, db, ObjectId, CronJob, request, fcm);
require('./routes/excelServer')(app, io, mongojs, db, ObjectId, CronJob, request, fcm, Excel, fs, path);

server.listen("8080", function() {
  console.log("Connected to db and listening on port 8080");
});

正如我所说console.log("Connected to db and listening on port 8080"); 在终端日志中可以看到。

当我尝试访问未定义的URL时,如下所示:

http://127.0.0.1:8080/asdasd

我想收到一条自动消息说:Cannot /GET asdasd 但我刚收到下一个错误:

This 127.0.0.1 page can’t be found
No webpage was found for the web address: http://127.0.0.1:8080/checkthis
HTTP ERROR 404

由于Apple的更新,我可能会重新安装node.js吗?

1 个答案:

答案 0 :(得分:1)

您缺少路线的正斜杠,其他一切正常,请检查:

app.get("/checkthis", function(req, res, next) {
  res.send("Yes");
});