求教 node for front-end developers 4章第一节 创建一个动态页面 form post时,我用源码的例子老报错
发布于 12 年前 作者 datianyun 3817 次浏览 最后一次编辑是 8 年前

页面就是

<form action="/" method="POST">
  <label>First name: 
    <input type="text" name="firstName" value="{{firstName}}" />
  </label>
  <label>Last name: 
    <input type="text" name="lastName" value="{{lastName}}" />
  </label>
  <input type="submit" value="Save" />
</form>

这块的action没有写 按我的理解 应该跟传统的web开发一样 指定一个对应的处理的JS文件吧,我写的相应的处理JS文件是书上的。

var connect = require(“connect”), fs = require(“fs”), mustache = require(“mustache”);

connect( connect.bodyParser(), function(req, res) { var userName = { firstName: req.body.firstName, lastName: req.body.lastName }, // create and open the stream tmplFile = fs.createReadStream( __dirname + “/public/edit.html”, {encoding: “utf8”} ), template = “”, html;

tmplFile.on("data", function(data) {
  template += data;
});
tmplFile.on("end", function() {
  
  // render the template with the userName object as data
  html = mustache.to_html(template, userName);
    
  res.end(html);

});

} ).listen(8000);

然后在浏览器打开页面,点击提交后报错 "NetworkError: 500 Internal Server Error - http://127.0.0.1:8020/node/serverJS/test.js"

一直不明白哪错了 ,希望大家帮忙看下 谢谢了

2 回复

listen 8000 , 访问 8020 端口 ~ ,囧

e …我明白了。。。我理解错了 我以为这个是讲submit数据处理的 。。。原来这节是说动态读取文件。。。。

回到顶部