Pug – Html preprocessor

Install

npm init -y
npm install express

Create main.js file

const { default: axios } = require('axios');
const express = require('express');
const app = express();

app.set('view engine', 'pug');

app.get('/', async (request, response) => {
    // response.send('hello');
    const getDb = await axios.get('http://localhost:3001/posts');
    console.log(getDb.data)
    response.render('index', { custdata: getDb.data } );
    console.log('active');
});

app.listen(3000, () => {
    console.log('Listening on port 3000');
}) 

 

Create API

npm install json-server ( or create global )

create data.json

{
    "posts": [
        { "id": 1, "title": "hello" },
        { "id": 2, "title": "hello2" }
    ],
    "profile": { "name": "typicode" }
} 

Setup Pug Engine

npm install pug

Tell Express to use pug

create data.json

app.set('view engine', 'pug'); 

the pug templates go to view folder

 

How to use CRUD requests

Followed this tutorial

Was this article helpful?

Related Articles

Leave A Comment?