Raz
Raz

Reputation: 1994

Localhost express routing does not work

I have a node project on localhost which works for months. Today there was an update on my Mac. Now when I am trying to access the node project via browser on localhost I just get a blank screen. But on my VPS the same project works as expected.

On terminal, there is log says that node is up and running but when I am trying to access some api I just got an error.

This is the main app.js file:

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");
});

As I said the console.log("Connected to db and listening on port 8080"); is seen in the Terminal log.

When I am trying to access an undefined URL like this:

http://127.0.0.1:8080/asdasd

I suppose to receive an automatic message saying: Cannot /GET asdasd But I just got the next error:

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

Maybe should I re-install node.js due to the update that Apple did?

Upvotes: 0

Views: 973

Answers (1)

kgangadhar
kgangadhar

Reputation: 5088

You are missing forward slash for route, everything else is working, check this:

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

Upvotes: 1

Related Questions