Express呈现index.html,但没有其他页面

时间:2018-10-17 02:34:52

标签: javascript node.js express

我有一个Express应用程序,只提供静态html文件。

let express = require('express');
let path = require('path');
let cookieParser = require('cookie-parser');
let logger = require('morgan');

let indexRouter = require('./routes/index');
let usersRouter = require('./routes/users');
let appRouter = require('./routes/app');

let app = express();

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('/users', usersRouter);
app.use('/app', appRouter);

module.exports = app;

/public中,我有index.htmlapp.html

/routes/中,我有index.jsapp.js

它们基本上是相同的

index.js

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

router.get('/', function (req, res, next) {
    res.render('index');
});

module.exports = router;

app.js

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

router.get('/', function (req, res, next) {
    res.render('app');
});

module.exports = router;

当我访问localhost:3000时,index.html文件会完美显示。

访问localhost:3000/app时收到错误消息

  

错误:未指定默认引擎,也未提供扩展名。       在新视图下(C:\ a \ Web \ todo \ node_modules \ express \ lib \ view.js:61:11)       在Function.render(C:\ a \ Web \ todo \ node_modules \ express \ lib \ application.js:570:12)       在ServerResponse.render(C:\ a \ Web \ todo \ node_modules \ express \ lib \ response.js:1008:7)       在C:\ Voliware \ Web \ todo \ routes \ app.js:5:9       在Layer.handle [作为handle_request](C:\ a \ Web \ todo \ node_modules \ express \ lib \ router \ layer.js:95:5)       在下一个(C:\ a \ Web \ todo \ node_modules \ express \ lib \ router \ route.js:137:13)       在Route.dispatch(C:\ a \ Web \ todo \ node_modules \ express \ lib \ router \ route.js:112:3)       在Layer.handle [作为handle_request](C:\ a \ Web \ todo \ node_modules \ express \ lib \ router \ layer.js:95:5)       在C:\ a \ Web \ todo \ node_modules \ express \ lib \ router \ index.js:281:22       在Function.process_params(C:\ a \ Web \ todo \ node_modules \ express \ lib \ router \ index.js:335:12)

2 个答案:

答案 0 :(得分:1)

您说过“只提供静态html文件”。然后标记以下3行。 // app.use('/', indexRouter); // app.use('/users', usersRouter); // app.use('/app', appRouter);

,然后访问access localhost:3000/app.html,而不访问access localhost:3000/app。因为您没有名为“ app”的静态文件。

答案 1 :(得分:0)

尝试将其添加到中间件堆栈中

dateFrom : '2018-10-10 14:50:16.479'