node.js - 在 Node.js 中执行 app.post 时出现 HTTP 404 错误

标签 node.js post express http-status-code-404 ejs

我是 Node.js 新手,尝试使用 Netbeans 通过在线教程进行学习。

当我这样做时:http://localhost:9080/我看到了预期的日期和颜色。但是当我尝试做http://localhost:9080/add时,要查看 app.post 部分,我收到 HTTP 404 错误。

谁能让我知道我做错了什么。

提前致谢,

ind.ejs

  <!doctype html>
  <html lang="en">
  <head>
    <meta charset="utf-8">
  </head>
  <body>
      <h1> my app </h1>
        <%= new Date() %>
        Color is: 
     <%= test %>
  </body>
  </html>

index.js:

'use strict';

module.exports = require('./lib/express');

var http = require('http');
var express = require('express');
var path = require('path');
var ejs = require('ejs');
var app = express();
var tropo_webapi = require('tropo-webapi');
var bodyParser = require('body-parser');

var test;

var app = express();

//app.use(bodyParser());

app.set('view engine','ejs');
app.set('views', path.join(__dirname,'views'));

app.get('/',function(req,res){
    var test = 'red';
    console.log('test in get is :' + test);
    res.render('ind.ejs',{test:test});
});

app.post('/add',function(req,res){
     test = 'blue';
    console.log('test in post is :' + test);
    res.render('ind.ejs',{test:test});
});

app.listen(9080, function(){
    console.log('Ready on port 9080');
});

最佳答案

app.get用于“GET”http 动词,默认使用该动词。 app.post被“POST”http 动词触发,可以使用以下形式来完成:

<form action="/add" method="post"><button type="submit">go</button></form>

关于node.js - 在 Node.js 中执行 app.post 时出现 HTTP 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32302655/

相关文章:

javascript - 使用 Nodejs 的 KafkaClient

node.js - 如何在 koa 中使用 thunk 获取 readStream ?

java - 验证发生时 + 符号从 xml 中删除

javascript - 向具有不同域和基本身份验证的服务器发出 ajax POST 请求是不可能的吗?

node.js - 声明合并不适用于 express 4.17.* 请求类型

javascript - 无法在 Express 4 中获取要通过请求发送的 cookie

iphone - 使用 iPhone 客户端和 Node.js 服务器进行 Facebook 或 Twitter 身份验证

node.js - Node.exe 卡在 100% CPU

html - 如何使用curl 发送包含隐藏表单字段的POST 表单?

node.js - Express/node.js 路由器中的通配符