Home > Software design >  Express js get request is showing on the html page
Express js get request is showing on the html page

Time:12-23

Im new in express.js so i would like to know why when I'm sending a data to client the data is showing in the browser but I'd like to send it in preview please can you take a look what I do wrong?

app.use(cors())
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));

app.get('/getProducts', (req,res) => {
  const obj = {
    data: 'jojo'
  };
  res.set('Content-Type','application/json');

  res.json(obj);
});

CodePudding user response:

first you need install dependencies like body-parse cors, then you need listen port like this

const express = require('express')
const cors = require('cors')
const bodyParser = require('body-parser')
const app = express()

app.use(cors())
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.get('/getProducts', (req, res) => {
    const obj = {
        data: 'jojo'
    };
    res.set('Content-Type', 'application/json');

    res.json(obj);
});

app.listen(3000)
  • Related