简体   繁体   中英

Express: Serving static files for defined routes

When I visit mysite.com/verify/myusn the result is a 404 error. When I visit mysite.com/ it serves me the index page as expected.

When I turned on the debugger, I realized Express was trying to serve a static file instead. But it also shows my route being registered properly. Please help me out.

Here is my debugger: 调试 1 调试 2 调试 3

Here is my server.js:

var express = require('express');
var app = express();
var path = require('path');

app.get('/*', function(req, res, next){ 
       next(); 
});
app.use(express.static(path.join(__dirname , '/_site/') , {maxAge:0}));
app.use('/assets/', express.static(path.join(__dirname , '/_site/assets') , {maxAge:0}));

var routesLogin = require(path.join(__dirname, '/api/routes/users'));
routesLogin(app);

app.get('/', function(req, res) {
  res.sendFile( path.join(__dirname , '/_site/landing.html'));
});

app.get('*', function(req, res) {
    res.send('404');
});

app.post('*', function(req, res) {
    res.send('404');
});


port = process.env.PORT || 3000;
app.listen(port);
console.log('listening on ' + port);

./api/routes/users.js:

module.exports = function(app){
var users = require('../controllers/userController');

app.route('verify/:usn')
    .get(users.verifyUSN)
};

./api/controllers/userController.js:

exports.verifyUSN = function(req, res, next){
res.status(200)
           .json({
               status: 'success',
               data: data,
               message: 'USN Verified.'
          });
}

I believe your problem could potentially be in /api/routes/users.js

module.exports = function(app){
  var users = require('../controllers/userController');

  // previously app.route('verify/:usn')
  app.route('/verify/:usn')
      .get(users.verifyUSN)
};

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM