I'm new to Express, but I keep getting a 404 error because something is making a GET request to '/json/version'. It goes through the first app.use('/')
and adds the session data, then it ignores the two routers, and then triggers the 404 error handling. Does anyone know where it could be coming from? It's holding up my project and I'm quite frustrated.
I found someone with the same issue here, but nothing has been very helpful (as far as I can tell). Most of this app is generated from express-generator
, with only the session being added by me, but I can't find any documentation from express-session
that says this is expected behavior. Below is my code.
// Package requirements
let createError = require('http-errors'),
express = require('express'),
session = require('express-session'),
path = require('path'),
cookieParser = require('cookie-parser'),
logger = require('morgan');
// Local requirements
let indexRouter = require('./routes/index'),
usersRouter = require('./routes/users');
// App definition
let app = express();
// View engine setup (EJS)
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// Session data
app.use(session({
secret: 'Super Secret',
}));
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', (req, res, next) => {
req.session.id = 1;
next();
});
app.use('/', indexRouter);
app.use('/', usersRouter);
// Catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// Error handler
app.use(function(err, req, res) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
// Specifies Node.js port
app.listen(3000, function() {
console.log('Listening on port 3000')
});
module.exports = app;
Thanks in advance!
Was addressed in this issue.
Click "Configure" in chrome://inspect
and remove port 3000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With