M
M
mrrangerr2020-06-21 14:37:59
Node.js
mrrangerr, 2020-06-21 14:37:59

How to attach a template to node js?

Hello everyone, I downloaded a free website template from the Internet, I want to attach this template to node js.
I implement the MVC pattern, put the template files into the views folder, corrected the imports, send the index.html file from the views folder in the controller, but nothing is displayed. The error No default engine was specified and no extension was provided. Tell me what am I doing wrong?

// app.js

const express = require('express')
const bodyParser = require("body-parser");
const config = require('config')
const homeRouter = require('./routes/homeRouter')

const app = express()
app.use('/', homeRouter)
const PORT = config.get('port') || 5000
app.listen(PORT, () => console.log(`App has been started on port ${PORT} `))

//homeControllers.js

exports.index = (req, res) => {
   
    res.render('index', function (err, html) {
        res.send(html)
      })

//homeRouter.js

const express = require('express')
const homeController = require('../controllers/homeControllers')
const homeRouter = express.Router()

homeRouter.get("/about", homeController.about);
homeRouter.get("/", homeController.index);
 
module.exports = homeRouter;


views folder structure
5eef4832d6a40717810526.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FinGanapre, 2020-06-21
@mrrangerr

You didn't specify which template engine you are using in the project. But, at the same time, you use the render method.
Here is an example from the documentation if pug is used: click
If you are using any other, then see how it connects. Express has examples for all popular templating engines, so there will be no problems :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question