0
votes

I used express-generator and the following are app.js:

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var cors = require('cors');

var app = express();
app.use(cors());
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

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')));

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

app.use('/', indexRouter);
app.use('/users', 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, next) {
  // 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');
});

module.exports = app;

And when access to http://localhost:5000/api/ got Error:

Not Found
404

NotFoundError: Not Found
    at D:\_projects\booking-system\api\app.js:28:8
    at Layer.handle [as handle_request] (D:\_projects\booking-system\api\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (D:\_projects\booking-system\api\node_modules\express\lib\router\index.js:317:13)

at D:_projects\booking-system\api\node_modules\express\lib\router\index.js:284:7 at Function.process_params (D:_projects\booking-system\api\node_modules\express\lib\router\index.js:335:12) at next (D:_projects\booking-system\api\node_modules\express\lib\router\index.js:275:10) at D:_projects\booking-system\api\node_modules\express\lib\router\index.js:635:15 at next (D:_projects\booking-system\api\node_modules\express\lib\router\index.js:260:14) at Function.handle (D:_projects\booking-system\api\node_modules\express\lib\router\index.js:174:3) at router (D:_projects\booking-system\api\node_modules\express\lib\router\index.js:47:12)

Following is routes/index:

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'API' });
});

module.exports = router;

Thank you very much for your helps !

1
Your code seems fine, we need to see your ./routes/index file.geauser
@geauser I've just updated with ./routes/indexQuang Le
You don't have any route with "/api". Access to http://localhost:5000/api/ return 404 as consequence. Try to access http://localhost:5000/.Đăng Khoa Đinh
@Đăng Khoa Đinh Thanks !Quang Le

1 Answers

0
votes

The issue is that you define a GET / route in your routes/index.js file and then register it as app.use('/', indexRouter) in your app.js.

So you either change your routes/index.js file to it looks like this:

var express = require('express');
var router = express.Router();

// Note the /api
router.get('/api', function(req, res, next) {
  res.render('index', { title: 'API' });
});

module.exports = router;

Or you keep routes/index as it is but your change the way your register it in your main.js to this:

// ...
// Note the /api
app.use('/api', indexRouter);
app.use('/users', usersRouter);
// ...