gpt4 book ai didi

javascript - Express 呈现index.html 但不呈现其他页面

转载 作者:行者123 更新时间:2023-12-03 00:55:44 27 4
gpt4 key购买 nike

我有一个 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时,我收到错误

Error: No default engine was specified and no extension was provided. at new View (C:\a\Web\todo\node_modules\express\lib\view.js:61:11) at Function.render (C:\a\Web\todo\node_modules\express\lib\application.js:570:12) at ServerResponse.render (C:\a\Web\todo\node_modules\express\lib\response.js:1008:7) at C:\Voliware\Web\todo\routes\app.js:5:9 at Layer.handle [as handle_request] (C:\a\Web\todo\node_modules\express\lib\router\layer.js:95:5) at next (C:\a\Web\todo\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (C:\a\Web\todo\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\a\Web\todo\node_modules\express\lib\router\layer.js:95:5) at C:\a\Web\todo\node_modules\express\lib\router\index.js:281:22 at Function.process_params (C:\a\Web\todo\node_modules\express\lib\router\index.js:335:12)

最佳答案

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

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

关于javascript - Express 呈现index.html 但不呈现其他页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52846491/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com