Javascript 什么是更精确的res.render(';test';)或res.render(';test.ejs';)?

Javascript 什么是更精确的res.render(';test';)或res.render(';test.ejs';)?,javascript,node.js,express,ejs,Javascript,Node.js,Express,Ejs,我是这样编码的 app.set('view engine', 'ejs'); app.get('/test', (req, res) => { res.render('show.ejs'); }) app.get('/test', (req, res) => { res.render('show'); }) 但几乎每个开发人员(和文档)都不会这样编写文件扩展名 app.set('view engine', 'ejs'); app.get('/test

我是这样编码的

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


app.get('/test', (req, res) => {
    res.render('show.ejs');
})


app.get('/test', (req, res) => {
    res.render('show');
})

但几乎每个开发人员(和文档)都不会这样编写文件扩展名

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


app.get('/test', (req, res) => {
    res.render('show.ejs');
})


app.get('/test', (req, res) => {
    res.render('show');
})

所以我尝试在viewsdir中创建两个ejs文件,(test.ejs)和(test.ejs.ejs)

第一次尝试

app.get('/test', (req, res) => {
    res.render('test'); //renders the first template (test.ejs)
})

第二次尝试

app.get('/test', (req, res) => {
    res.render('test.ejs'); //renders the first template (test.ejs)
})

第三次尝试

app.get('/test', (req, res) => {
    res.render('test.ejs.ejs'); //renders the second template (test.ejs.ejs)
})

我被搞糊涂了,我比所有人都准确!!?,所以我的问题是为什么文档不写文件扩展名,有什么区别吗


我知道,如果您查看Express代码内部,不太可能出现这种情况-您将发现以下内容:

/**
 * Register the given template engine callback `fn`
 * as `ext`.
 *
 * By default will `require()` the engine based on the
 * file extension. For example if you try to render
 * a "foo.ejs" file Express will invoke the following internally:
 *
 *     app.engine('ejs', require('ejs').__express);
 *
 * For engines that do not provide `.__express` out of the box,
 * or if you wish to "map" a different extension to the template engine
 * you may use this method. For example mapping the EJS template engine to
 * ".html" files:
 *
 *     app.engine('html', require('ejs').renderFile);
 *
 * In this case EJS provides a `.renderFile()` method with
 * the same signature that Express expects: `(path, options, callback)`,
 * though note that it aliases this method as `ejs.__express` internally
 * so if you're using ".ejs" extensions you dont need to do anything.
 *
 * Some template engines do not follow this convention, the
 * [Consolidate.js](https://github.com/tj/consolidate.js)
 * library was created to map all of node's popular template
 * engines to follow this convention, thus allowing them to
 * work seamlessly within Express.
 *
 * @param {String} ext
 * @param {Function} fn
 * @return {app} for chaining
 * @public
 */

app.engine = function engine(ext, fn) {
  if (typeof fn !== 'function') {
    throw new Error('callback function required');
  }

  // get file extension
  var extension = ext[0] !== '.'
    ? '.' + ext
    : ext;

  // store engine
  this.engines[extension] = fn;

  return this;
};
这是指向该文件的链接:

据我所知,如果在代码中设置视图引擎

app.set('view engine', 'ejs')
然后,当您不添加扩展时,Express会自动为您完成,因为它知道要使用什么引擎,从而知道正确的扩展

但是如果您没有设置引擎,那么Express将使用扩展来获取所需的引擎

因此,基本上如果没有设置引擎视图,则需要添加扩展
但是如果您确实设置了它,那么如果您想添加扩展名,它是可选的,因为express将自动为您完成它

如果您查看express代码内部,您将发现以下内容:

/**
 * Register the given template engine callback `fn`
 * as `ext`.
 *
 * By default will `require()` the engine based on the
 * file extension. For example if you try to render
 * a "foo.ejs" file Express will invoke the following internally:
 *
 *     app.engine('ejs', require('ejs').__express);
 *
 * For engines that do not provide `.__express` out of the box,
 * or if you wish to "map" a different extension to the template engine
 * you may use this method. For example mapping the EJS template engine to
 * ".html" files:
 *
 *     app.engine('html', require('ejs').renderFile);
 *
 * In this case EJS provides a `.renderFile()` method with
 * the same signature that Express expects: `(path, options, callback)`,
 * though note that it aliases this method as `ejs.__express` internally
 * so if you're using ".ejs" extensions you dont need to do anything.
 *
 * Some template engines do not follow this convention, the
 * [Consolidate.js](https://github.com/tj/consolidate.js)
 * library was created to map all of node's popular template
 * engines to follow this convention, thus allowing them to
 * work seamlessly within Express.
 *
 * @param {String} ext
 * @param {Function} fn
 * @return {app} for chaining
 * @public
 */

app.engine = function engine(ext, fn) {
  if (typeof fn !== 'function') {
    throw new Error('callback function required');
  }

  // get file extension
  var extension = ext[0] !== '.'
    ? '.' + ext
    : ext;

  // store engine
  this.engines[extension] = fn;

  return this;
};
这是指向该文件的链接:

据我所知,如果在代码中设置视图引擎

app.set('view engine', 'ejs')
然后,当您不添加扩展时,Express会自动为您完成,因为它知道要使用什么引擎,从而知道正确的扩展

但是如果您没有设置引擎,那么Express将使用扩展来获取所需的引擎

因此,基本上如果没有设置引擎视图,则需要添加扩展
但是如果您确实设置了扩展名,那么如果您想添加扩展名,那么它是可选的,因为express会自动为您完成它

我想逻辑很简单:如果没有扩展名,则添加文件扩展名。在第二个和第三个例子中,有一个。我想逻辑很简单:如果没有扩展名,就添加文件扩展名。在你的第二个和第三个案例中,有一个。