如何修复routers.get和app.use无法在快递中正常工作?

时间:2019-02-08 21:51:55

标签: node.js express

我使用express-generator创建了一个新的express项目,并想添加一个子路由系统,例如以下“ localhost /”将带我进入索引,而“ localhost / projects”将发送“ projects页面”和“ localhost / project / randomCircles”将发送“圈子”。

节点版本:v10.15.1

快速版本:4.16.0

My Project directory

还尝试使randomCircles直接路线有效,但我不知道有什么区别

**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 indexRouter = require('./routes/index');
var projectsRouter = require('./routes/projects');
var circlesRouter = require('./routes/randomCircles');

var app = express();

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

app.use('/', indexRouter);
app.use('/projects', projectsRouter);
app.use('/randomCircles', circlesRouter);

// 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;





**index.js**:
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'ProjectManager',menu: ['About Me','Projects','Contact Me']});
});
module.exports = router;

**project.js**:
var express = require('express');
var router = express.Router();

/* GET projects list. */
router.get('/', function (req, res, next) {
    res.send('projects page');
});
router.get('/randomCircles', function (req, res, next) {
    res.send('circles');
});
module.exports = router;

**randomCircle.js*:

var express = require('express');
var router = express.Router();
/* GET project randomCircles. */
router.get('/', function(req, res, next) {
    res.render('randomCircles', {title: 'randomCircles'});
});
module.exports = router;

有效的路由:

  • 本地主机/
  • localhost / randomCircles

不起作用的路由:  -本地主机/项目  -本地主机/项目/ randomCircles

我应该能够访问“ localhost / projects”,但出现此错误:

未找到 404 NotFoundError:找不到     在C:\ Users \ josea \ Documents \ MEGAsync \ coding \ projectManager \ app.js:27:8     在Layer.handle上[作为handle_request](C:\ Users \ josea \ Documents \ MEGAsync \ coding \ projectManager \ node_modules \ express \ lib \ router \ layer.js:95:5)     在trim_prefix(C:\ Users \ josea \ Documents \ MEGAsync \ coding \ projectManager \ node_modules \ express \ lib \ router \ index.js:317:13)     在C:\ Users \ josea \ Documents \ MEGAsync \ coding \ projectManager \ node_modules \ express \ lib \ router \ index.js:284:7     在Function.process_params(C:\ Users \ josea \ Documents \ MEGAsync \ coding \ projectManager \ node_modules \ express \ lib \ router \ index.js:335:12)     在下一个(C:\ Users \ josea \ Documents \ MEGAsync \ coding \ projectManager \ node_modules \ express \ lib \ router \ index.js:275:10)     在C:\ Users \ josea \ Documents \ MEGAsync \ coding \ projectManager \ node_modules \ express \ lib \ router \ index.js:635:15     在下一个(C:\ Users \ josea \ Documents \ MEGAsync \ coding \ projectManager \ node_modules \ express \ lib \ router \ index.js:260:14)     在Function.handle(C:\ Users \ josea \ Documents \ MEGAsync \ coding \ projectManager \ node_modules \ express \ lib \ router \ index.js:174:3)     在路由器上(C:\ Users \ josea \ Documents \ MEGAsync \ coding \ projectManager \ node_modules \ express \ lib \ router \ index.js:47:12)

路由系统中有一个错字,编辑了帖子的代码,现在可以正常工作了<3

1 个答案:

答案 0 :(得分:0)

您还需要使用app.listen

  • 配置路由器对于具有功能性 Express 应用是必不可少的,但您有时还需要 listen 传入请求。在您的 服务器起点的某处
  • 要导入并集成在另一个文件中定义的路由器,请首先将其导入。然后使用app.use(myOtherRouter)
  • 添加它

const express = require('express')
const app = express()
const port = 3000

// Import your other router(s)
const someRouter = require('./myOtherRouter');

app.use(someRouter);

// Listen for incoming requests
app.listen(port, () => console.log(`Example app listening on port ${port}!`))