Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Express未使用正确的ect.js根目录_Node.js_Express_Ectjs - Fatal编程技术网

Node.js Express未使用正确的ect.js根目录

Node.js Express未使用正确的ect.js根目录,node.js,express,ectjs,Node.js,Express,Ectjs,我已经准备好了一个快速的express.js应用程序,正在尝试使用它。但是,无论我怎么做,express都会不断尝试将文件放在错误的位置。我已经按照主页上的指示去做了 Myserver.js: import express from 'express'; import path from 'path'; import ECT from 'ect'; const port = 3000; const app = express(); const renderer = ECT({ watch

我已经准备好了一个快速的express.js应用程序,正在尝试使用它。但是,无论我怎么做,express都会不断尝试将文件放在错误的位置。我已经按照主页上的指示去做了

My
server.js

import express from 'express';
import path from 'path';
import ECT from 'ect';

const port = 3000;
const app = express();

const renderer = ECT({
  watch: true,
  root: __dirname + '/src',
  ext: '.html'
});
app.set('view engine', 'ect');
app.engine('ect', renderer.render);

console.log(renderer.render('index'));

app.listen(port, function(error) {
  if (error) {
    console.log(error);
  } else {
    console.log("Listening on port " + port + "...");
  }
});

app.get('/', function(req, res) {
  res.render('index');
});
console.log
从正确的位置打印出正确的文件。但是,尝试在浏览器中访问
/
会导致:

Error: Failed to lookup view "src/index" in views directory "/home/david/taekwondo2/views"
    at Function.render (/home/david/taekwondo2/node_modules/express/lib/application.js:10:10)
...
另外,我还安装了
节点sass中间件


为什么express不公平?

你好,亲爱的朋友,我想你只是犯了一个小错误,当我看到自述文件时,我发现了一些问题,我认为你应该这样解决:

如自述:

从“express”导入express;
从“路径”导入路径;
从“ECT”导入ECT;
常数端口=3000;
常量app=express();
常量渲染器=ECT({
手表:没错,
根目录:uu dirname+'/src',

ext:'.ect'//这没有什么区别。保留为空也不能解决问题。express仍在尝试从
视图
读取文件。此外,我在将提供的代码复制到问题中时犯了一个小错误(我在
res.render
中有一个额外的
src
),我已经删除了它。
import express from 'express';
import path from 'path';
import ECT from 'ect';

const port = 3000;
const app = express();

const renderer = ECT({
  watch: true,
  root: __dirname + '/src',
  ext: '.ect' // <== this must be same as your template engine format (.ect)
});
app.set('view engine', 'ect');
app.engine('ect', renderer.render);

console.log(renderer.render('index'));

app.listen(port, function(error) {
  if (error) {
    console.log(error);
  } else {
    console.log("Listening on port " + port + "...");
  }
});

app.get('/', function(req, res) {
  res.render('src/index');
});