简体   繁体   English

如何在 express.js 中添加路由到子文件夹?

[英]How to add routing to sub folders in express.js?

I'm trying to break my routes.js file into separate files so it's easier to work with.我正在尝试将我的 routes.js 文件分成单独的文件,以便更容易使用。

Here is my folder structure这是我的文件夹结构

routes.js
views/
├─ 1537/
│  ├─ prototype-4/
│  │  ├─ _routes.js
│  │  ├─ report.html

routes.js路由.js

const express = require('express')
const router = express.Router()

router.use('/1537/prototype-4', require('./views/1537/prototype-4/_routes'));

module.exports = router

views/1537/prototype-4/_routes.js意见/1537/prototype-4/_routes.js

const express = require('express')
const router = express.Router()

router.get('/report/:reportId', function (req, res) {
    console.log("Hello world");
    res.render("report");
});

module.exports = router

When I run the following it returns 'hello world' but it can't find the template.当我运行以下命令时,它返回“hello world”,但找不到模板。

template not found: report.html未找到模板:report.html

Can someone please point me in the right direction?有人可以指出我正确的方向吗?

Just needed to use nodejs' __dirname to resolve the path in views/1537/prototype-4/_routes.js只需要使用nodejs的__dirname来解析views/1537/prototype-4/_routes.js中的路径

const express = require('express')
const router = express.Router()

router.get('/report/:reportId', function (req, res) {
    console.log("Hello world");
    res.render(__dirname + "/report");
});

module.exports = router

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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