无法获得使用参数的明确路由的错误:
当我第一次尝试使用参数表达路由时,我会收到错误,我不知道为什么。
它运行良好,我安装了lodash,现在什么都没有。
这是代码:
app.get("/posts/:postName"), function (req, res) {
let requestedTitle = req.params.postName;
posts.forEach(function(post) {
let storedTitle = post.title;
if (storedTitle === requestedTitle) {
console.log('match found');
} else {
console.log('no match found');
}
});
};
我正在使用localhost,当我尝试运行它时,它会给我一个错误。
例如,我输入: http:// localhost:3000/posts/test 通常,它使我在家庭路线上,如果它与我之前的一个条目相匹配,则将控制台记录下来。
我已经安装了Express,所有这些都是完整的代码。但是它在安装Lodash后就无法正常工作了,你们在这里看到错误吗?我现在盯着这个问题大声笑:
//jshint esversion:6
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const _ = require('lodash');
const homeStartingContent = "text";
const app = express();
let posts = [];
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
app.get("/", function(req, res) {
res.render(__dirname + "/views/home.ejs", {homeStartingContent: homeStartingContent, posts: posts});
});
app.get('/about', (req, res) => {
res.render('about', {aboutContent: aboutContent});
});
app.get('/contact', (req, res) => {
res.render('contact', {contactContent: contactContent});
});
app.get('/compose', (req, res) => {
res.render('compose');
});
app.post('/compose', (req, res) => {
posts.push(req.body);
console.log(posts)
res.redirect('/')
});
app.get("/posts/:postName"), function (req, res) {
let requestedTitle = req.params.postName;
posts.forEach(function(post) {
let storedTitle = post.title;
if (storedTitle === requestedTitle) {
console.log('match found');
} else {
console.log('no match found');
}
});
};
app.listen(3000, function() {
console.log("Server started on port 3000");
});
在明确的文档中,它显示了这个代码示例,我觉得我的代码肯定应该起作用。我不明白为什么不是。
app.get('/users/:userId/books/:bookId', (req, res) => {
res.send(req.params)
})
I'm getting a Cannot GET error when I try express routing with parameters for the first time and I don't know why.
It worked fine and I installed lodash and now nothing works anymore.
Here`s the code:
app.get("/posts/:postName"), function (req, res) {
let requestedTitle = req.params.postName;
posts.forEach(function(post) {
let storedTitle = post.title;
if (storedTitle === requestedTitle) {
console.log('match found');
} else {
console.log('no match found');
}
});
};
I'm using Localhost and when I try to run it it gives me a Cannot Get error.
For example I enter: http://localhost:3000/posts/test
And usually it kept me on the home route and console logged if it matched one of my entries before or not.
I have Express installed and everything heres the complete code. But it just won't work anymore after installing lodash, do you guys see an error somewhere here? I'm staring at this for 3 days now lol:
//jshint esversion:6
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const _ = require('lodash');
const homeStartingContent = "text";
const app = express();
let posts = [];
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
app.get("/", function(req, res) {
res.render(__dirname + "/views/home.ejs", {homeStartingContent: homeStartingContent, posts: posts});
});
app.get('/about', (req, res) => {
res.render('about', {aboutContent: aboutContent});
});
app.get('/contact', (req, res) => {
res.render('contact', {contactContent: contactContent});
});
app.get('/compose', (req, res) => {
res.render('compose');
});
app.post('/compose', (req, res) => {
posts.push(req.body);
console.log(posts)
res.redirect('/')
});
app.get("/posts/:postName"), function (req, res) {
let requestedTitle = req.params.postName;
posts.forEach(function(post) {
let storedTitle = post.title;
if (storedTitle === requestedTitle) {
console.log('match found');
} else {
console.log('no match found');
}
});
};
app.listen(3000, function() {
console.log("Server started on port 3000");
});
In the Express documentation it showed this code example and I feel my code should definitely work. I don't understand why it is not.
app.get('/users/:userId/books/:bookId', (req, res) => {
res.send(req.params)
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
注意闭合括号:
它不应该在那里,而应该在处理程序功能的末尾。
Notice the closing parentheses:
It shouldn't be there, but at the end of the handler function.