Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to parse using express without body parser

const express = require('express')

const app = express()

app.use(express.json())
app.use(express.urlencoded())
Comment

express bodyparser

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

const app = express()

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

app.use(function (req, res) {
  res.setHeader('Content-Type', 'text/plain')
  res.write('you posted:
')
  res.end(JSON.stringify(req.body, null, 2))
})

Comment

express bodyparser

/** @format */

const express = require("express");
const app = express();
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const PORT = process.env.PORT || 3000;

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));

// parse application/json
app.use(bodyParser.json());

//connecting to db
try {
    mongoose.connect('mongodb://localhost/YOUR_DB_NAME', {
        useNewUrlParser: true,
        useUnifiedTopology: true,
      	useCreateIndex: true,
      }, () =>
      console.log("connected"));
  } catch (error) {
    console.log("could not connect");
  }

app.get("/", (req, res) => {
  res.send("home");
});

app.listen(PORT, () => console.log(`Server is listening on port ${PORT}`));
Comment

body-parser vs express.json

// Express v4.16.0 onwards
const express = require('express');
// ---
app.use(express.json());


// Before Express v4.16.0
const express = require('express');
const bodyParser = require('body-parser');
// ---
app.use(bodyParser.json());
Comment

Install body-parser for Express

npm install body-parser --save
Comment

PREVIOUS NEXT
Code Example
Javascript :: ejs variable 
Javascript :: opening a link in another tab in react 
Javascript :: js convert html to text 
Javascript :: javascript if has jquery loaded 
Javascript :: rect p5js 
Javascript :: js remove item from array by value 
Javascript :: url in js 
Javascript :: ajax load document ready 
Javascript :: convert data into json format in javascript 
Javascript :: react js create element 
Javascript :: convert date to string format dd/mm/yyyy javascript 
Javascript :: get placeholder innertext 
Javascript :: avascript sum of arguments 
Javascript :: crop image canvas 
Javascript :: current date minus days javascript 
Javascript :: calculato 
Javascript :: get first object key javascript 
Javascript :: get last element in an array jqury 
Javascript :: express return json 
Javascript :: react useeffect 
Javascript :: how to remove key value pair from object js 
Javascript :: clear swr cache 
Javascript :: killall node windows 
Javascript :: get random percentage javascript 
Javascript :: date format in react js 
Javascript :: call laravel route js 
Javascript :: local storage ha 
Javascript :: react for loop in render 
Javascript :: js loop through array backwards with foreach 
Javascript :: format money javascript 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =