与node.js的get/post混淆,并试图创建一个新页面

Confusion with node.js get/post and trying to make a new page

本文关键字:创建 新页面 一个 js node get 混淆 post      更新时间:2024-03-20

我整个晚上都在寻找如何做到这一点,但我不知道该搜索哪些术语,所以这让我有点倒退。。。

我正在尝试在node.js应用程序中创建一个名为"/editor"的页面。我在我的索引页面上创建了一个按钮,我想将其重定向到/editor。我也制作了一个editor.jade文件,但我不知道如何导航到它

在app.js中我有:

app.get('/', routes.index);
app.get('/game', routes.game);
app.get('/getContents', routes.getContents);
app.get('/editor', routes.editor);
app.post('/start', routes.start);
app.post('/quit', routes.quit);
app.post('/doAction', routes.doAction);

这应该是一个得到吗?还是我感到困惑,它实际上应该是一个帖子?

在index.js中我有:

function editor(req, res){
    console.log("Hey Sarah!");
    if(req.session.editor){
        console.log("Hey Sarah!");
    }
    else{
        console.log("Do you eve know what you're doing?");
    }
}
exports.editor = editor;

在index.jade中我有:

  div.well
    p Adventure Game Editor
    div
      form(action="/editor". method="post")
         div.control-group.input-append
         button(type ="editor") Editor

现在点击按钮只会将我重定向到/发布?哪个显示Cannot GET/post?以及控制台中的GET/post?404 2ms

浏览一下,您的form(action="/editor". method="post")似乎有一个句点而不是逗号(它们看起来非常相似,行为也非常不同)。我不确定翡翠会怎么处理,但这可能是你犯错的原因。如果你想让张贴的表格放在任何地方,你还需要一行app.post("/editor", FUNC_HERE)。我会将按钮更改为读取button(type="submit"),因为editor不是有效的html按钮类型。