我在遵循基于NodeJS,Express,Jade和MongoDB的教程时遇到了令人沮丧的问题。此问题已在此论坛的其他主题中讨论过,但即使按照已发布的建议,我也无法使POST
命令生效。
不要考虑Jade缩进,在复制+粘贴中可能有点不对,但在实际代码中是正确的。
当我在我的input.js页面中POST
时,该网站会向我抛出404错误。我知道这不是数据库问题,因为我可以很好地在其他页面上输出数据。
我输入的jade模板。
extends head
block content
div(id="input_page_wrapper")
div(id="input")
form(id="input_form", name="addEntry", method="post" action="/addEntry")
div(class="label_box")
label(for="restname") Restaurant Name:
input(type="text", placeholder="Example: Soul Kitchen", name="restname")
div(class="label_box")
label(for="restdesc") Restaurant Description:
input(type="text", placeholder="Write a short description.", name="restdesc")
div(class="label_box")
label(for="restdesc") Restaurant Address:
input(type="text", placeholder="Example: Fleminginkatu 26", name="restadd")
button(id="submit", name="submit", type="submit") submit
我的input.js路线
var express = require('express');
var router = express.Router();
/* GET Input page. */
router.get('/', function(req, res) {
res.render('input', { title: 'Input to Database' });
});
/* POST to Add User Entry */
router.post("/addEntry", function(req, res) {
// Set our internal DB variable
var db = req.db;
// Get our form values. These rely on the "name" attributes
var restName = req.body.restname;
var restDesc = req.body.restdesc;
var restAdd = req.body.restadd;
console.log("post received: %s %s", restName, restDesc, restAdd);
// Set our collection
var collection = db.get('restaurants');
// Submit to the DB
collection.insert({
"name" : restName,
"desc" : restDesc,
"address" : restAdd
}, function (err, doc) {
if (err) {
// If it failed, return error
res.send("There was a problem adding the information to the database.");
}
else {
// And forward to success page
res.redirect("/input");
}
});
});
module.exports = router;
我的App.js文件
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/nodetest1');
var routes = require('./routes/index');
var input = require('./routes/input');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// Make our db accessible to our router
app.use(function(req,res,next){
req.db = db;
next();
});
app.use('/', routes);
app.use('/input', input);
/// catch 404 and forwarding to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
整个源代码也可以在GitHub中查看。 https://github.com/ValterAndersson/DinnerApp
答案 0 :(得分:1)
我认为你需要在表单中缩进你的标签,试试这个,并错过一个','在表格标签中(方法="后"):
string siteContent = "";
using (System.Net.WebClient client = new System.Net.WebClient())
{
client.Encoding = System.Text.Encoding.UTF8;
siteContent = client.DownloadString(edtReadFromUrl.Text);
}