gpt4 book ai didi

javascript - 为什么不显示 ' admin' 页面?

转载 作者:行者123 更新时间:2023-12-03 06:23:52 26 4
gpt4 key购买 nike

我正在阅读《MEAN MACHINE》一书,并按照其说明进行操作路由 Node 应用程序 [第 - 36 页]

The express.Router()⁴⁸ acts as a mini application. You can call an instance of it (like we do for Express) and then define routes on that. Let’s look at an example so we know exactly what this means. Add this to your ‘server.js’ file if you’d like to follow along. Underneath our app.get() route inside of server.js, add the following. We’ll 1. call an instance of the router 2. apply routes to it 3. and then add those routes to our main app

因此,我复制粘贴了代码[请参见下文] http://localhost:1337/admin页面抛出错误“Cannnot GET/admin”

代码:

// load the express package and create our app
var express = require('express');
var app = express();
var path = require('path');

// send our index.html file to the user for the home page
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});

// create routes for the admin section
// get an instance of the router
var adminRouter = express.Router();
// route middleware that will happen on every request
// admin main page. the dashboard (http://localhost:1337/admin)
adminRouter.get('/', function(req, res) {
res.send('I am the dashboard!');
});

// users page (http://localhost:1337/admin/users)
adminRouter.get('/users', function(req, res) {
res.send('I show all the users!');
});

// posts page (http://localhost:1337/admin/posts)
adminRouter.get('/posts', function(req, res) {
res.send('I show all the posts!');
// apply the routes to our application
app.use('/admin', adminRouter);
});

// start the server
app.listen(1337);
console.log('1337 is the magic port!');

最佳答案

app.use('/admin', adminRouter); 将 adminRouter 添加到主应用程序。

您在该路由器上内部调用/posts的函数中拥有该函数。

因此,它永远不会被调用。

您需要将其向下移动,以便它出现在 });//start the server

之间

关于javascript - 为什么不显示 ' admin' 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38742410/

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