错误:未指定默认引擎,也未提供扩展名。 (以Express为框架)

时间:2019-07-30 04:27:23

标签: javascript node.js express

我看到了很多标题相同的问题,它们确实在某种程度上帮助了我,但实际上我从未摆脱过这个错误。

为您的背景信息,我创建了一个名为listing.js的文件作为运行节点命令的平台。 我试图通过 listing.js 使用node.js来提供一个名为 weatha-picka.html 的网页,以及由 weatha-picka组成的外部组件。 js weatha-picka.css。

这是上面提到了上面3个文件的文件夹树: Click here to see the picture.

以下是我在 listing.js 中编写的用于启动服务器的内容:

// Module dependencies 
var application_root = __dirname,
path = require( 'path' ); 
express = require( 'express' ); 

// Creating server // 
var app = express();  

// Configurating the server   
app.get('/', function (req, res) {
  res.render('index', {});
});  
app.use( express.static( path.join( application_root, 'site')));  

// Starting the server 
app.listen(3500, () => {console.log("hold my Mac")}); 

即使我以前尝试过body-parser之类的中间件,但错误消息在localhost:3500上仍然相同。

  

错误:未指定默认引擎,也未提供扩展名。       在新视图下(/Users/quanvihong/node_modules/express/lib/view.js:61:11)       在Function.render(/Users/quanvihong/node_modules/express/lib/application.js:570:12)       在ServerResponse.render(/Users/quanvihong/node_modules/express/lib/response.js:1012:7)       在/Users/quanvihong/Desktop/Weathers/server/listing.js:11:7       在Layer.handle [作为handle_request](/Users/quanvihong/node_modules/express/lib/router/layer.js:95:5)       在下一个(/Users/quanvihong/node_modules/express/lib/router/route.js:137:13)       在Route.dispatch(/Users/quanvihong/node_modules/express/lib/router/route.js:112:3)       在Layer.handle [作为handle_request](/Users/quanvihong/node_modules/express/lib/router/layer.js:95:5)       在/Users/quanvihong/node_modules/express/lib/router/index.js:281:22       在Function.process_params(/Users/quanvihong/node_modules/express/lib/router/index.js:335:12)

总而言之,最迫切的问题是错误是来自listing.js的简单性还是来自文件层次结构? 我对Node.js很陌生,因此任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:0)

我没有看到用于服务器渲染的任何const formData = new FormData(); formData.append('nameOfTheKeyYouWantToSetThisDataTo', myFileInput.files[0], 'avatar.jpg'); 视图。尝试创建一个fetch(url, { method: 'POST', body: formData }).then(function (response) { ... }); 作为主页,并在index中对其进行更正。

index.html

如果您想将listing.js用作静态页面,只需将其返回为:

app.get('/', (req, res) => {
    res.sendFile(__dirname + '/index.html');
});

此外,如果要创建更多视图作为weatha-picka.html,请尝试使用ejs模板模块。下面是ejs和express的示例。

app.get('/weatha-picka', (req, res) => { res.sendFile(__dirname + '/weatha-picka.html'); }); 中:

weatha-picka

listing.js文件夹中,创建const express = require('express'); var app = express(); app.set('view engine', 'ejs'); app.set('views', path.join(__dirname, './public')); // ejs engine looks for ejs files in public folder app.get('/weatha-picka', function (req, res) { res.render('weatha-picka', {}); }); 模板文件:

/public