admin管理员组文章数量:1406312
I am developing an expressjs app using ejs templating engine. But i want to use keep the extension of ejs file like home.html instead of using home.ejs. The reason for this i am using visual studio for development and visual studio doesn't support ejs file. so code hinting formatting and highlighting doesn't work.
var express = require("express");
var app = express();
app.set("view engine", "ejs");//setting ejs view engine
app.set("views", "./views");
app.get("/", function (req, res) {
res.render("home");
});
app.listen(3000, function () {
console.log("Server is listening on port 3000");
})
I am developing an expressjs app using ejs templating engine. But i want to use keep the extension of ejs file like home.html instead of using home.ejs. The reason for this i am using visual studio for development and visual studio doesn't support ejs file. so code hinting formatting and highlighting doesn't work.
var express = require("express");
var app = express();
app.set("view engine", "ejs");//setting ejs view engine
app.set("views", "./views");
app.get("/", function (req, res) {
res.render("home");
});
app.listen(3000, function () {
console.log("Server is listening on port 3000");
})
Share
Improve this question
edited Dec 24, 2014 at 9:28
Naeem Shaikh
15.7k7 gold badges52 silver badges91 bronze badges
asked Dec 9, 2014 at 15:54
manasmanas
6,41010 gold badges49 silver badges57 bronze badges
2
-
2
Why would you do this instead of configuring Visual Studio so that it knows
.ejs
files should be treated as.html
? It seems like this is asking the wrong question. – loganfsmyth Commented Dec 9, 2014 at 16:37 - i think @loganfsmyth is right, you need to configure your VS to acknowledge .ejs file so it's treated that file as a html. – Eko Junaidi Salam Commented May 6, 2020 at 8:05
4 Answers
Reset to default 2I'm manage to solve this issue , my code similar to @homam answer, but I'll write the code more pletely
const http = require('http');
const path = require('path');
const express = require('express');
const engine = require('ejs');
const app = express();
app.engine('html', engine.__express);
app.set('views', path.join(__dirname, './views'));
app.set('view engine', 'html');
//this route will open profil.html in folder ./views
app.get('/', (req, res) => {
//render file profil.html
res.render('profil', {
name: 'Danang Ponorogo'
});
});
var server = http.createServer(app);
server.listen(3001, () => {
console.log('Server is running at port 3001');
});
Hope it help. Thanks.
You can use app.engine(ext, callback)
Set the following up with your view engine and views settings near the top:
app.engine('html', require('ejs').renderFile);
Then when you call res.render
the only difference is that you need to specify the extension:
app.get('/',function(req,res){
res.render("home.html", { myString: "I'm a normal string!" });
});
You'll find that <%=myString%>
is handled as you would expect it to in an .ejs file.
Use app.engine('.html', require('ejs').__express);
of ejs npm module:
app.engine('.html', require('ejs').__express);
app.set('view engine', 'ejs');
I had similar issue with using Visual Studio for working with ejs files. The best way, I believe is to configure Visual Studio to interpret ejs file as html by going to menu - TOOLS-->Options-->Text Editor-->FileExtension and add ejs file extension against HTML Editor. Screenshot
本文标签: javascriptIs there a way to keep the file extension of ejs file as htmlStack Overflow
版权声明:本文标题:javascript - Is there a way to keep the file extension of ejs file as .html? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745005688a2637247.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论