Try running this command in command prompt:
express --help
It will give you the express generator help:
Usage: express [options] [dir]
Options:
-h, --help output usage information
--version output the version number
-e, --ejs add ejs engine support
--hbs add handlebars engine support
--pug add pug engine support
-H, --hogan add hogan.js engine support
--no-view generate without view engine
-v, --view <engine> add view <engine> support (ejs|hbs|hjs|jade|pug|twig|vash) (defaults to jade)
-c, --css <engine> add stylesheet <engine> support (less|stylus|compass|sass) (defaults to plain css)
--git add .gitignore
-f, --force force on non-empty directory
Source: https://expressjs.com/en/starter/generator.html
The above options give you the list of "view engines".
Now, simply type:
express -{your choice view engine}
For example using express -e:
This sets the EJS engine as your view handler and removes jade. EJS has the look and feel of HTML with the added ability to inject values via their template system.
Answer from Harsha Kasturi on Stack OverflowTry running this command in command prompt:
express --help
It will give you the express generator help:
Usage: express [options] [dir]
Options:
-h, --help output usage information
--version output the version number
-e, --ejs add ejs engine support
--hbs add handlebars engine support
--pug add pug engine support
-H, --hogan add hogan.js engine support
--no-view generate without view engine
-v, --view <engine> add view <engine> support (ejs|hbs|hjs|jade|pug|twig|vash) (defaults to jade)
-c, --css <engine> add stylesheet <engine> support (less|stylus|compass|sass) (defaults to plain css)
--git add .gitignore
-f, --force force on non-empty directory
Source: https://expressjs.com/en/starter/generator.html
The above options give you the list of "view engines".
Now, simply type:
express -{your choice view engine}
For example using express -e:
This sets the EJS engine as your view handler and removes jade. EJS has the look and feel of HTML with the added ability to inject values via their template system.
If you won't a view engine just type:
express --no-view
You can add an engine after or not working with server rendering.
Videos
Β» npm install express-generator
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
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
Hi all. I am certain this is a stupid question.. apologies in advance.
Express help contains the following:
> express --help
Usage: express [options] [dir]
Options:
--version output the version number
-e, --ejs add ejs engine support
--pug add pug engine support
--hbs add handlebars engine support
-H, --hogan add hogan.js engine support
-v, --view <engine> add view <engine> support (dust|ejs|hbs|hjs|jade|pug|twig|vash) (defaults to jade)
--no-view use static html instead of view engine
-c, --css <engine> add stylesheet <engine> support (less|stylus|compass|sass) (defaults to plain css)
--git add .gitignore
-f, --force force on non-empty directory
-h, --help output usage information
My question is, what is the difference between the -e option and the -v option? I've been searching for days and I simply cannot find a clear answer to this. Please, somebody, explain and help put me out of my misery !
Passing this as the answer
Try res.sendFile(path.join(__dirname + '/../public/index.html'))
If you're starting from scratch, you can run:
npm install -g express-generator # to install express-generator globally
express projectName --no-view --git # '--git' is optional and generates a '.gitignore' file
Β» npm install express-generator-typescript
Hi,
I am learning express but I don't want to use ejs, pug or JSX as view engine.
Since javaScript provides string template, I want to use it.
I use the express-generator template.
/view/index.js
`<h1>${title}</h1>`
`<p> Welcome to ${title}</p>`/app.js
var indexRouter = require('./routes/index');
...
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'js');
...
app.use('/', indexRouter);/routes/index.js
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
module.exports = router;
However when I node ./bin/www I get this error message:
Error: Cannot find module 'js'
Require stack:
- /home/capeya/localLibrary/node_modules/express/lib/view.js
- /home/capeya/localLibrary/node_modules/express/lib/application.js
- /home/capeya/localLibrary/node_modules/express/lib/express.js
- /home/capeya/localLibrary/node_modules/express/index.js
- /home/capeya/localLibrary/app.js
- /home/capeya/localLibrary/bin/www
at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
at Module._load (node:internal/modules/cjs/loader:985:27)
at Module.require (node:internal/modules/cjs/loader:1235:19)
at require (node:internal/modules/helpers:176:18)
at new View (/home/capeya/localLibrary/node_modules/express/lib/view.js:81:14)
at Function.render (/home/capeya/localLibrary/node_modules/express/lib/application.js:587:12)
at ServerResponse.render (/home/capeya/localLibrary/node_modules/express/lib/response.js:1039:7)
at /home/capeya/localLibrary/app.js:38:7
at Layer.handle_error (/home/capeya/localLibrary/node_modules/express/lib/router/layer.js:71:5)
at trim_prefix (/home/capeya/localLibrary/node_modules/express/lib/router/index.js:326:13)Tried to look for a solution online but I found nothing about setting javascript files as view engine in express.
When I remove app.set('view engine', 'js'); then I get
Error: No default engine was specified and no extension was provided.