DEV Community

A.R
A.R

Posted on

Difficulties associated with the MERN stack #MERNdevelopment #MERNdev #ReactJS #NodeJS

_**Learning Curve:

Developers who are new to the MERN stack or certain technologies within it may face a steep learning curve. Each component (MongoDB, Express, React, Node) has its own set of concepts and best practices to master.**_

// Creating a MongoDB connection and inserting a document

const { MongoClient } = require('mongodb');

const uri = 'mongodb://localhost:27017';
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });

async function run() {
  try {
    await client.connect();
    console.log('Connected to MongoDB');

    const database = client.db('mydatabase');
    const collection = database.collection('mycollection');

    const document = { name: 'John Doe', age: 30 };
    const result = await collection.insertOne(document);
    console.log(`Inserted document with _id: ${result.insertedId}`);
  } finally {
    await client.close();
  }
}

run();


Enter fullscreen mode Exit fullscreen mode
// Creating a simple Express server

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.send('Hello, Express!');
});

app.listen(port, () => {
  console.log(`Express server listening at http://localhost:${port}`);
});

Enter fullscreen mode Exit fullscreen mode
// Creating a simple React component

import React from 'react';

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = { message: 'Hello, React!' };
  }

  render() {
    return <div>{this.state.message}</div>;
  }
}

export default MyComponent;

Enter fullscreen mode Exit fullscreen mode
// Creating a basic Node.js server

const http = require('http');
const port = 3000;

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello, Node.js!');
});

server.listen(port, () => {
  console.log(`Node.js server listening on port ${port}`);
});

Enter fullscreen mode Exit fullscreen mode

Top comments (0)