0% found this document useful (0 votes)
7 views4 pages

Simple Node.js API and File Operations

Uploaded by

feret82429
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Simple Node.js API and File Operations

Uploaded by

feret82429
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Make a simple API

 index file:-

const http = require('http');


const data = require('./[Link]')

[Link]((req, res) => {

[Link](200, { 'Content-Type': 'application\JSON' });


[Link]([Link](data));
[Link]();

}).listen(3000);

 data file:-

const data = [
{ name: 'Dhruvi Dhameliya', email: 'dhruvi@[Link]' },
{ name: 'Dhruvi Dhameliya', email: 'dhruvi@[Link]' },
{ name: 'Dhruvi Dhameliya', email: 'dhruvi@[Link]' }
];
[Link] = data;
Getting input from command line:-

 Set input from command line :-

[Link]([Link]);

 File Write :-

const fs = require('fs');

const input = [Link];

[Link](input[2],input[3]);

 Add and Remove file :-

if (input[2] == 'add') {
[Link](input[3], input[4])
}
else if (input[2] == 'remove') {
[Link](input[3])
}
else {
[Link]("invalid input");
}
Show file list

 Create File :-

const fs = require('fs');

[Link]('[Link]','const demo = "file"');

 To Create File In Folder Using Loop :-

const fs = require('fs');
const path = require('path');
const dirpath = [Link](__dirname, 'files');

[Link](dirpath);
for (i = 0; i <= 5; i++) {

//[Link]("hello"+i+".txt" , "Welcome to simba Institute.......");


[Link](dirpath + `/demo ${i}.js`, 'const demo = "file"');

 To Read File From Directory :-

[Link](dirpath, (err, files) => {


[Link](files);
})

 Foreach Loop :-

[Link](dirpath, (err, file) => {


[Link]((item) => {
[Link]("file name is ", item);
})
})

You might also like