Search code examples
javascriptangularjsnode.jsexpressurl-routing

Express Routing: 404 on some route names, but not others?


I'm making a single page app with Angular routes on the front end, but the backend is giving me grief.

All the routes that involve interfacing with the database work, but when I add in a route (Copying the exact same structure) to send an html file, or just hello world, it returns a 404, not even registering on the node console (with one weird exceptions noted below).

I read this post explaining how static should come after routes, it makes sense, but it's not working on my code.

app.js is as follows:

    // SETUP
var express = require('express');
var app = express();
var router = express.Router();
var mongoose = require('mongoose');
var port = process.env.PORT || 3001;
var database = 'mongodb://example-database...';
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');


// CONFIG 
mongoose.connect(database);

// Note: response parser must be placed before routing
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.set('jsonp callback name', 'cb');

app.use(logger('dev'));
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(router);

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
  next();
});

// ROUTES
require("./routes/api_chirp.js")(router);
require("./routes/api_video.js")(router);
require("./routes/api_interaction.js")(router);
require("./routes/api_user.js")(router);
require("./routes/api_search.js")(router); 
require("./routes/api_jsonp.js")(router);
require("./routes/index.js")(router);


app.use(express.static(path.join(__dirname, 'public')));

// ERROR HANDLING

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

module.exports = app;

app.listen(port);
console.log("App listening on port " + port);

index.js (Note: The same structure used in other working API routes (using an alternative route, i.e. "/api/someroute")

module.exports = function(router) {
  router.route('/workspace') // Only works if route is prefixed with "api_someword", "someword" itself won't work.
    .get(function(req, res) {
      console.log('request received');
      res.send('hello world');
    }); // Ends - Post

};

Question: I'm at the end of my rope... ANY suggestions, answers will help at this point. How do I get that route in index.js working?


Solution

  • Solved:

    Another developer putting a path filter on nginx that prevented any url that didn't have isn't prefixed with /api to get through to node. I added it and it solved the problem.