Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js + Express without using Jade

Is it possible to use express without any template engine?

like image 917
prog keys Avatar asked Sep 22 '11 19:09

prog keys


People also ask

Can you use node js without Express?

You cannot use Express without NodeJS by definition so you have to deploy your backend somewhere else in you want to use it.

Do I need to learn NodeJS before ExpressJS?

Before diving into Express. js, you will want to learn how to write a basic HTTP server in plain Node. js. Since Express is just an extension on top of the native built-in HTTP server, knowing how to handle a request/response cycle will make jumping into basic Express applications very simple.

Can express js be used for front end?

Yes, Node. js can be used in both the frontend and backend of applications.


3 Answers

Yes,

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

should just work

like image 166
Marcel M. Avatar answered Oct 20 '22 09:10

Marcel M.


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'));
like image 31
Robert Brisita Avatar answered Oct 20 '22 08:10

Robert Brisita


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"); 
});
like image 7
JGallardo Avatar answered Oct 20 '22 10:10

JGallardo