Debug School

rakesh kumar
rakesh kumar

Posted on

how to create dir, read and using path module in node js and express

To create a directory, read a file, and write a file using Node.js and Express, you can utilize the built-in fs (file system) module. Here's an example that demonstrates these operations:

const express = require('express');
const fs = require('fs');
const app = express();

// Create a directory
app.get('/createDir', (req, res) => {
  const directoryName = 'my-directory';

  fs.mkdir(directoryName, (err) => {
    if (err) {
      console.error(err);
      return res.status(500).send('Failed to create directory');
    }

    res.send('Directory created successfully');
  });
});

// Read a file
app.get('/readFile', (req, res) => {
  const fileName = 'sample.txt';

  fs.readFile(fileName, 'utf8', (err, data) => {
    if (err) {
      console.error(err);
      return res.status(500).send('Failed to read file');
    }

    res.send(data);
  });
});

// Write a file
app.get('/writeFile', (req, res) => {
  const fileName = 'sample.txt';
  const content = 'Hello, World!';

  fs.writeFile(fileName, content, (err) => {
    if (err) {
      console.error(err);
      return res.status(500).send('Failed to write file');
    }

    res.send('File written successfully');
  });
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

In this example, we define three routes to demonstrate the operations: /createDir, /readFile, and /writeFile.

  1. /createDir creates a new directory named 'my-directory' using fs.mkdir(). If there's an error, it logs the error and sends a 500 response. Otherwise, it sends a success message.
  2. /readFile reads the content of a file named 'sample.txt' using fs.readFile(). If there's an error, it logs the error and sends a 500 response. Otherwise, it sends the file's content.
  3. /writeFile writes the content 'Hello, World!' to a file named 'sample.txt' using fs.writeFile(). If there's an error, it logs the error and sends a 500 response. Otherwise, it sends a success message . Please note that you need to have the necessary permissions and ensure that the specified directory and file exist or have appropriate write/read permissions, respectively. You can modify the example as needed to suit your use case.

Create dir, read and write file using path module

To create a directory, read a file, and write a file using the path module in Node.js and Express, you can combine it with the fs (file system) module. The path module helps manipulate file and directory paths. Here's an example that demonstrates these operations:

const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();

// Create a directory
app.get('/createDir', (req, res) => {
  const directoryName = 'my-directory';
  const directoryPath = path.join(__dirname, directoryName);

  fs.mkdir(directoryPath, (err) => {
    if (err) {
      console.error(err);
      return res.status(500).send('Failed to create directory');
    }

    res.send('Directory created successfully');
  });
});

// Read a file
app.get('/readFile', (req, res) => {
  const fileName = 'sample.txt';
  const filePath = path.join(__dirname, fileName);

  fs.readFile(filePath, 'utf8', (err, data) => {
    if (err) {
      console.error(err);
      return res.status(500).send('Failed to read file');
    }

    res.send(data);
  });
});

// Write a file
app.get('/writeFile', (req, res) => {
  const fileName = 'sample.txt';
  const filePath = path.join(__dirname, fileName);
  const content = 'Hello, World!';

  fs.writeFile(filePath, content, (err) => {
    if (err) {
      console.error(err);
      return res.status(500).send('Failed to write file');
    }

    res.send('File written successfully');
  });
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

In this example, we import the path module and use its join() function to create the file or directory path by joining the directory name or file name with __dirname, which represents the current directory.

  1. /createDir creates a new directory named 'my-directory' using fs.mkdir() and the path created with path.join(). If there's an error, it logs the error and sends a 500 response. Otherwise, it sends a success message.
  2. /readFile reads the content of a file named 'sample.txt' using fs.readFile() and the path created with path.join(). If there's an error, it logs the error and sends a 500 response. Otherwise, it sends the file's content.
  3. /writeFile writes the content 'Hello, World!' to a file named 'sample.txt' using fs.writeFile() and the path created with path.join() . If there's an error, it logs the error and sends a 500 response. Otherwise, it sends a success message. By utilizing the path module, you can create file and directory paths in a platform-independent manner and perform file system operations effectively.

Image description

Image description

Image description

Top comments (0)