Is it possible to use express without any template engine?
-
2The template engines in Express are OPTIONAL. You can just create a static file server for the pages you've already made. – ampersand Sep 22 '11 at 20:33
-
Possible duplicate of [Using HTML in Express instead of Jade](http://stackoverflow.com/questions/11495595/using-html-in-express-instead-of-jade) – Gus Crawford Sep 27 '16 at 22:56
6 Answers
Yes,
app.get('/', function(req, res){
res.render('index.html');
});
should just work

- 2,376
- 2
- 18
- 24
-
13I tried using this approach, but keep getting "Cannot find module 'html' " error. I tried giving an explicit path as well as putting the html file in the same dir as the app, to no avail. Any ideas? – Eran Rehavi Jul 25 '12 at 11:53
-
1You need to remove the view engine options from the `configure` block of Express. – Golo Roden Jan 02 '13 at 17:42
-
4for `Cannot find module 'html'` error, read this -> http://stackoverflow.com/q/4529586/104380 – vsync May 05 '13 at 12:40
-
1
-
@EranRehavi, the file needs to be in public, not views. Moving index.html to the root public folder solved it for me. – Anish May 19 '16 at 03:19
-
@Anish - that might be becuase express.static() defined only for the public folder, try defining the same for the views folder and *.html files would work at views folder too. – Abilash Arjunan May 20 '16 at 10:47
UPDATED
Some might have concerns that sendFile only provides client side caching. There are various ways to have server side caching and keeping inline with the OP's question one can send back just text too with send:
res.send(cache.get(key));
Below was the original answer from 3+ years ago:
For anyone looking for an alternative answer to PavingWays, one can also do:
app.get('/', function(req, res) {
res.sendFile('path/to/index.html');
});
With no need to write:
app.use(express['static'](__dirname + '/public'));

- 5,461
- 3
- 36
- 35
-
1sendfile is now deprecated; sendFile is recommended and requires an absolute path or the 'root' option set. But this is the correct answer and it should be selected as such. – kirk.burleson Sep 07 '14 at 19:53
-
Writing 4 lines to serve ONE file is a good alternative to 1 line serving ALL files because ... ? Oh and it's deprecated now too ;) Who is voting on such answers here? – Rocco Sep 20 '16 at 21:44
-
This answer http://stackoverflow.com/a/20580051/489396 explains why using sendFile is not a good idea, and the general question has other even better answers there. – Gus Crawford Sep 27 '16 at 22:11
-
@GusCrawford you do realize that you are marking down an answer that is almost three years old and was valid in that point in time? – Robert Brisita Sep 27 '16 at 22:18
-
I do- I also voted down the most popular answer also which isn't relevant anymore. – Gus Crawford Sep 27 '16 at 22:56
-
@GusCrawford: Just a thought but maybe you should forgo down voting old answers and just up vote new relevant ones? It'll save posters time revisiting old answered questions as to why it was down voted. These posts are timestamped and technical users will know it probably isn't relevant. – Robert Brisita Sep 28 '16 at 14:31
-
@RobertBrisita Just a thought but maybe as opposed to saving the poster time figuring out why their answer "isn't useful" maybe edit the answer and share the last version of Express it applied to? It'll save developers time filtering through answers that may or may not be useful. Most of us are technical enough to read dates, 'old' in and of itself doesn't always mean not-relevant. – Gus Crawford Sep 28 '16 at 17:11
-
@GusCrawford you are more than welcome to edit and update. I feel your first comment was enough without the need to down vote. – Robert Brisita Sep 28 '16 at 17:18
-
@RobertBrisita I would genuinely love to do that / plan to / will delete our smart-ass comment-versation :) / will vote up – Gus Crawford Sep 28 '16 at 17:33
For anyone having the need to immediately use regular HTML without jade in a new express project, you can do this.
Add a index.html
to the views folder.
In app.js
change
app.get('/', routes.index);
to
app.get('/', function(req, res) {
res.sendfile("views/index.html");
});
UPDATE
Use this instead. See comment section below for explanation.
app.get('/', function(req, res) {
res.sendFile(__dirname + "/views/index.html");
});

- 11,074
- 10
- 82
- 96
-
4Just to update this answer, sendfile was deprecated and is now sendFile (notice the capital F) and would be written like this: res.sendFile(__dirname + "/views/index.html"); It takes an absolute path or needs the 'root' option set. – kirk.burleson Sep 07 '14 at 19:47
-
Might as well put a script on any Express related comment that kicks in after 6 months and says- this technique has been deprecated. – Hairgami_Master May 14 '15 at 20:40
-
1**This is not working now. **, express returns a error while changing it. – Akansh May 19 '16 at 17:09
-
This answer http://stackoverflow.com/a/20580051/489396 explains why using sendFile is not a good idea, and the general question has other even better answers there. – Gus Crawford Sep 27 '16 at 22:11
-
@GusCrawford well the answer exists and has helped others. Feel free to suggest an edit. – JGallardo Sep 28 '16 at 18:40
You can serve static files automatically with Express like this:
// define static files somewhere on top
app.use(express['static'](__dirname + '/your_subdir_with_html_files'));
Actually this should be express.static(...) but to pass JSLint above version works too ;)
Then you start the server and listen e.g. on port 1337:
// app listens on this port
app.listen(1337);
Express now serves static files in /your_subdir_with_html_files automatically like this:

- 91
- 7
This is all out of date - correct answer for 3x, 4x is
The second answer here: Render basic HTML view?
-
... which is exactly the same as Emanuel Saringan's answer below, the one with a current score of -2 – Rocco Sep 20 '16 at 21:46
-
In your main file:
app.get('/', function(req, res){
res.render('index');
});
Your index.jade file should only contain:
include index.html
where index.html is the raw HTML you made.
-
1With this he would still be using Jade. Also the roundabout nature of this solution is unnecessarily hack-y and adds weight. – May 15 '14 at 06:39
-