Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
I can't post on server?

JAVASCRIT, REACT, Node.js As you can see from the javascript code I am trying to add some on the server-side using method post but it can not post on the server. What I can change on my code.**

        const express = require('express');
const cors = require('cors');
const { MongoClient, ServerApiVersion, ObjectId } = require('mongodb');
require('dotenv').config()
const app = express();

app.use(cors());
app.use(express());
                app.post('/items', async (req, res) => {
                    const newItem = req.body;
                    console.log(newItem);
                    const result = await laptopCollection.insertOne(newItem);
                    res.send(result);
                })

I think I am missing in middleware

    app.use(cors());
app.use(express());
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I think correct middleware is:

app.use(cors())
app.use(express.json())

Post with async function

async function run() {
    try {
        await client.connect();
const laptopCollection=client.db('database_name').collection('collection_name');

       app.post('/items', async (req, res) => {
                    const newItem = req.body;
                    console.log(newItem);
                    const result = await laptopCollection.insertOne(newItem);
                    res.send(result);
                })
        // Add New Post
    } finally {
        // await client.close();
    }
}
run().catch(console.dir);

about 4 years ago · Juan Pablo Isaza Report

0

app.use(express()) function can't parses incoming JSON requests and also can't puts the parsed data in req.body.To solve the problem write the function as

app.use(express.json())

You also forget to import async function. You need to provide this codes:

async function run() 
{
  try {
    await client.connect();
    const laptopCollection=client.db('database_name').collection('collection_name');
  app.post('/items', async (req, res) => {
    const newItem = req.body;
    console.log(newItem);
    const result = await laptopCollection.insertOne(newItem);
    res.send(result);
  })
} 
about 4 years ago · Juan Pablo Isaza Report

0

Maybe the correct way is:

app.use(cors())
app.use(express.json())
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!