Nodejs提取帖子数据并以HTML格式显示

时间:2015-08-12 06:31:48

标签: html node.js html5

我需要从post数据中获取值,哪些应该使用nodejs显示在HTML中,在php中我们会像下面这样做

<p><?php echo $_POST['data']; ?></p>

我们如何在nodejs中做同样的事情?是否可以使用玉?如果不是我们如何使用玉器

2 个答案:

答案 0 :(得分:1)

body-parserEmbeddedJS

一起使用

以下是使用ExpressJS + EmbeddedJS

的示例
//Setting up things for POST Param parsing with a limit to 15mb POST data.
app.use(bodyParser.json({limit: '15mb'})); // to support JSON bodies
app.use(bodyParser.urlencoded({ extended: true, limit:'15mb' })); // to support URL-encoded bodies

app.post('/test', function(req, res) {
  res.render('test.ejs', {post_data: req.body});
});

//Now in the EJS file you set :
<p><%= post_data.data %></p>

答案 1 :(得分:0)

在客户端,我假设您正在拨打这样的电话,

  $.ajax({
    url: "domain.com/post/url", 
    type: "POST",
    data: JSON.stringify({formField : $("#formfield").val()}),
    contentType:"application/json; charset=utf-8",
    dataType:"json",
    success: function(data){
    },
    error: function(xhrObj){
    }})

然后在后端,当您使用jade渲染视图时,请执行此操作

app.use( bodyParser.json() )
api.post("/post/url", function(req, res, next){
  res.render("iamaview", {"iamavar": req.body.formField})
})

其中app是expressJs应用,api是快速路由器

在app / views / iamaview.jade中,使用iamavar作为变量,

#{iamavar}