console.log我的表单数据到我的服务器时得到错误

时间:2017-11-30 18:36:39

标签: node.js forms express post body-parser

我正在尝试console.log我的数据从表单发送到/ person endpoint.But获取错误未定义或错误:未指定默认引擎且未提供扩展名。

ERROR

listening
undefined
Error: No default engine was specified and no extension was provided.
 at new View (/home/mohit/edx-
proj/node_modules/express/lib/view.js:61:11)
at EventEmitter.render (/home/mohit/edx-
proj/node_modules/express/lib/application.js:570:12)
at ServerResponse.render (/home/mohit/edx-
proj/node_modules/express/lib/response.js:1008:7)
at /home/mohit/edx-proj/server.js:42:9
at Layer.handle [as handle_request] (/home/mohit/edx-
proj/node_modules/express/lib/router/layer.js:95:5)
at next (/home/mohit/edx-
proj/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/mohit/edx-
proj/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/mohit/edx-
proj/node_modules/express/lib/router/layer.js:95:5)
at /home/mohit/edx-
proj/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/mohit/edx-
proj/node_modules/express/lib/router/index.js:335:12)

server.js文件

var express = require('express');
var sqlite3 = require('sqlite3');
var formidable = require('formidable');
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false })
var app=express();
app.get(`/`,function(request,response){
      response.writeHead(200, {'Content-Type': 'text/html'});
      response.write("sfewqcfnvsjv")
      response.write('<form action="/persons" method="post" >');
      response.write('<input type="number" name="filetoupload"><br>');
      response.write('<input type="text" name="filetoupload"><br>');
      response.write('<input type="text" name="filetoupload"><br>');
      response.write('<input type="submit">');
      response.write('</form>');
      response.end()
 });

app.post('/persons',urlencodedParser,function(req, res){
    console.log(req.body);
    res.render('persons',{qs:req.query});
});

app.listen(8000,function(){
    console.log("listen:8000")
})

请让我知道我错过了什么,并提前感谢你。

1 个答案:

答案 0 :(得分:1)

您尚未指定哪个视图引擎

是:

以HTML格式呈现

app.set('view engine', 'html');

甚至以json格式

res.json({ error: err })

或在ejs中渲染

app.set('view engine', 'ejs');

或用jade / pug渲染

app.set('view engine', 'jade');

app.set('view engine', 'pug');

注意:您要使用 jade pug ,具体取决于文件的格式,而不是{{1 }} app.set('view engine', 'jade');在一起,您可以阅读有关jadepug

的更多信息

您需要指定。