[英]ExpressJS route not working?
I am using express with hoganjs templating instead of jade. 我正在使用带有hoganjs模板而不是翡翠的express。
When I try to access one of my routes, it wont work though... 当我尝试访问我的一条路线时,它仍然无法工作...
In app.js, I have the following (relevant to the route): 在app.js中,我有以下内容(与路由有关):
var awesome = require('./routes/awesome.js');
app.use('/awesome', awesome);
In the routes/awesome.js file, I have the following: 在routes / awesome.js文件中,我具有以下内容:
var express = require('express');
var router = express.Router();
/* GET awesome page. */
router.get('/awesome', function(req, res, next) {
res.render('awesome', { title: "awesome", message: "awesome"});
});
module.exports = router;
And lastly, here is my awesome template (located in ./views/awesome.hjs). 最后,这是我很棒的模板(位于./views/awesome.hjs中)。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{title}}</title>
<meta name="Author" content="{{author}}"/>
<link rel="shortcut icon" href="" />
<!-- Bootstrap CSS-->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<!-- Bootstrap JS -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-xs-12">
<div class="jumbotron">
<h1>{{ title }}</h1>
<p>Welcome to {{ title }}, here is your message: {{message}}</p>
</div>
</div>
</div>
</div>
</body>
</html>
I have basically the same code for the index route so why wont this one work too? 我的索引路由基本上具有相同的代码,那么为什么也不能这样做呢?
In awesome.js change the router path location to /
在awesome.js中,将路由器路径位置更改为
/
router.get('/', function(req, res, next) {
res.render('awesome', { title: "awesome", message: "awesome"});
});
Now localhost:3000/awesome
should be available. 现在
localhost:3000/awesome
应该可用了。
app.use('/awesome', ...)
matches all the routes starting with awesome
. app.use('/awesome', ...)
匹配所有以awesome
开头的路由。 Paths specified in router.get('/awesome', ...)
acts as sub-paths. router.get('/awesome', ...)
指定的路径充当子路径。 The path URL will be localhost:3000/awesome/awesome
. 路径URL将为
localhost:3000/awesome/awesome
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.