【发布时间】:2017-06-08 17:52:54
【问题描述】:
我刚开始使用 node.js,但我不太明白一些事情。所以请不要吝啬。我尝试在网上搜索,但没有太多结果。
我希望一个网页继续等待帖子,然后呈现收到的数据。 到目前为止,我有以下 app.js
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing
application/x-www-form-urlencoded
app.set('view engine', 'pug');
app.get('/', function (req, res) {
res.render('index', {title: 'GET test',message : 'GET test'});
});
var data = "";
app.post("/",function(req, res) {
console.log(req.method);
console.log(req.headers);
console.log(req.url);
data=req.body
console.log(data);
res.render('index', {title: 'POST test',message : 'POST test'});
});
app.listen(8081, function () {
console.log('Example app listening on port 8081!');
});
在views/index.pug我有
html
head
title = title
body
h1 = message
每当我触发curl --data "field=value" http://127.0.0.1:8081/
我可以看到终端内收到的帖子,但我不明白如何在页面中呈现这些数据。
【问题讨论】:
标签: javascript node.js curl