Reputation: 21
I'm writing an Express application without a template engine I am using just using HTML
as the template engine.
app.set('view engine', 'html');
actually, the whole code was generated using express-generator and I set the view to --no-view
Flag and the index URL page runs well but trying another URL like users or any other except the index URL does work all it shows is
No default engine was specified and no extension was provided.
But if I Add the above code app.set('view engine', 'html');
it returns
Error: Cannot find module 'html'
. Below are the codes.
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var addressRouter = require('./routes/address');
var app = express();
app.set('view engine', 'html');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
module.exports = app;
And the userRouter user.js
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/', function(req, res, next) {
res.render('index');
});
module.exports = router;
For Home Page
router.get('/', function(req, res, next) {
res.render('index');
});
Can someone please tell what is wrong here.
Upvotes: 0
Views: 3402
Reputation: 21
What I did was with sendFile
on the route controller like the userRoute controller
res.sendFile(path.resolve(__dirname,'../public/index.html'));
and I remove the set engine
app.set('view engine', 'html');
But the index page works perfectly without the above tweak.
Upvotes: 0
Reputation: 36319
As @Henrique said, you are already able to render staticHTML. What’s tripping you up is the res.render calls. Those specifically exist to call a template. Switch to res.send and your error should go away.
Upvotes: 0
Reputation: 680
To serve html you do not need to use the view engine, just use express.static, as you are already using
app.use (express.static (path.join (__ dirname, 'public')));
Upvotes: 2