Complete-BACKEND in One Video- 6PP

ยท

1 min read

๐Ÿ”— MERN-definiton & roadmap Guide

0. Handling Errors :

1. import

import mongoose from 'mongoose';    ๐Ÿ’€

Solution:

// In package.json file add
{
    .......... ,
    "type": "module",
}

2. Tips for fast exectuion

{
    // In package.json file add
    "scripts": {
    ............. ,
    "start": "node index.js",
    "dev": "nodemon index.js"
  },
}

3. res.sendFile(path.join(__dirname + '/about.html'))

Error: __dirname is not defined โŒ

Soln.

import { fileURLToPath } from 'url';
import { dirname } from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

1. How do I start with Node.js after I installed it?

  • Once we have installed Node.js, let's build our first web server. Create a file named app.js containing the following contents:
const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
  • Now, run your web server using node app.js.

  • Visit localhost:3000 and you will see a message saying "Hello World".

ย