I use the express generator build a no-view rest api first. Then I change the whole things to es6 script and let it compile by babel.
When I enter the localhost after the changes, it says:
No default engine was specified and no extension was provided
And I found out it's because of this part of code.
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
The error disappear when I change the res.render to res.json
I just wondering do I have to assign a view engine if I use the babel to compile my project? Even It's a no view api?
To use views on express you need to configure it to use some engine (pug, ejs, handlebars), refer to the documentation to know how it works.
The error that you mentioned
No default engine was specified and no extension was provided
means that you didn't configure any view engine, also, the template file is missing the extension, (i.g. index.pug).
When you use res.render('index', { title: 'Express' }); you are calling the view engine to render the template index and passing some values to use in it { title: 'Express' }, the result is a HTML content.
On other hand, when you use res.json({ title: 'Express' }) you are returning a json object, that's the difference.
Here is the link to the documentation of each one: