节点JS - 如果不是'/'就不能POST?

时间:2017-06-13 17:12:01

标签: javascript json node.js post get

我不知道这里有什么问题,因为我跟着像Node JS cannot post error?这样的问题,并试图完成一个非常简单的任务 - 只需控制台.log一个表格POSTS。

我的问题是(我正在使用快递),如果我的表单的动作不是'/',我会收到错误:Cannot POST /search

我的表格:

<form id="demo-2" method = "POST" action="/">
    <input type="search" name = "search" placeholder="Search">
                        </form>

。我的index.js:

var express = require('express');
var pg = require('pg');
var bodyParser = require('body-parser');
var app = express();

app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/views/'));

// views is directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

//app.use(express.bodyParser());


//DIFFERENT APPS - tells them what to do

app.post('/', function(request, response) {
    //var username = req.body;

    console.log("posted something");
    response.end("something");
});

app.post('/search', function(request, response) {
    //var username = req.body;

    console.log("posted something");
    response.end("something2");
});

app.get('/search', function(request, response) {
     response.send("skylarr");

});

app.get('/', function(request, response) {
     //response.send("skylarr");
  response.render('pages/index');

我同时为两者设置POST和GET功能,但我不明白为什么/ search POST在/ search GET时不起作用。

0 个答案:

没有答案