Reputation: 8004
I do not want Jade or EJS on my site. How can I create an express site without it defaulting to the Jade templates? Thanks
Upvotes: 32
Views: 35861
Reputation: 1006
There is much simpler way to create a view without templating engine. The most basic way to do it is using ES6 method of template literal and res.send()
, in your Express app. With this, you can inject a static template or dynamic template based on use cases.
Static Template
const template = `
<div>Hello World</div>
`;
res.send(template);
Dynamic Template
Literally using template literal and different route, you can create a default template with dynamically created template injection and styling based on a page's unique need. You may also modularize your application for code reusability.
For more info visit: Source Code | Demo
PS: In this manner, you have lesser dependencies needed for your application [The POC above only has 2 dependencies, ie: Nodemon and Express], hence reducing the risk of breaking changes and have time tested application. No Public folder needed.
Upvotes: 0
Reputation: 671
The easiest way to generate express app without view is:
npx express-generator --no-view
It will generate the following folder structure and files:
Upvotes: 0
Reputation: 1924
You could use commands below to install express-generator globally and then scaffold a project without a view engine
npm install -g express-generator
express newProject --no-view
Upvotes: 28
Reputation: 29
Use Restify
http://restify.com/
var restify = require('restify'),
fs = require('fs');
var server = restify.createServer({
certificate: fs.readFileSync('path/to/server/certificate'),
key: fs.readFileSync('path/to/server/key'),
name: 'MyApp',
});
server.listen(8080);
It borrows heavily from Express -Routing
Upvotes: 1
Reputation: 4941
With Express 4.0.0, the only thing you have to do is comment out 2 lines in app.js:
/* app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade'); */ //or whatever the templating engine is.
And then drop your static html into the /public directory. Example: /public/index.html
Upvotes: 2
Reputation: 14103
If what you want is directly to serve static html files with the possibility to cache resources, while still being able to hit "/" and get index.html, then the answer is as easy as this:
var express = require('express');
var http = require('http');
var app = express();
app.use(express.static(__dirname + '/public'));
http.createServer(app).listen(3000);
Gotcha: Html files including index.html must be inside /public folder instead of /views
Upvotes: 32
Reputation: 11072
You can comment out the lines
app.set 'views', __dirname + '/views'
app.set 'view engine', 'jade'
from the Express initialization code.
If you are serving only static content: https://github.com/visionmedia/express/blob/master/examples/static-files/index.js
Otherwise, use your database, your files, your user input, or whatever to concatenate a string that will make up the http response.
// Express 3.x
app.get('*', function(req,res){
fs.readFile('./foo.txt', 'utf8', function (err, data) {
if (err) throw err;
data += (req.query['something'] || "")
res.type('text/plain');
res.send(200, data);
});
});
With that said: I have grown to love Jade as I've been playing with it for the past few months. It has its idiosyncracies but it's orders of magnitude faster to write any complicated html.
Upvotes: 5