我是 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/