节点和快速路由 - 404错误

时间:2017-01-16 19:21:07

标签: node.js express morgan

我使用nodemon收回404错误,但不太确定问题出在哪里。任何提示/资源赞赏!

项目根

$ curl http://127.0.0.1:3000/v1/protected
Cannot GET /v1/protected

nodemon:"GET /v1/protected/ HTTP/1.1" 404 26 "-" "curl/7.49.1"

index.js

var express = require('express');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');

var app = express();

var router = require('./services/router');

mongoose.connect('mongodb://localhost:introToBackend/introToBackend');

app.use(morgan('combined'));
app.use(bodyParser.json());
app.use(express('/v1', router));

const PORT = process.env.PORT || 3000;
var HOST = process.env.HOST || '127.0.0.1';

console.log('Listening on', HOST, PORT);
app.listen(PORT, HOST);

服务/ router.js

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

function protectedRoute(req, res, next) {
  res.send("The secret!");
}

router.route('/protected')
  .get(protectedRoute);

module.exports = router;

1 个答案:

答案 0 :(得分:0)

尝试写作 express.use(' / V1',yourRouter); 你不需要app.use中的快递