0% found this document useful (0 votes)
4 views9 pages

Node.js File Operations and Server Setup

The document contains multiple JavaScript files demonstrating various functionalities, including file system operations, server creation with Express, and streaming data. It showcases how to read and write files, create and delete directories, and set up an HTTP server that serves HTML files based on URL requests. Additionally, it includes examples of using lodash for random number generation and function invocation control.

Uploaded by

h4838543
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)
4 views9 pages

Node.js File Operations and Server Setup

The document contains multiple JavaScript files demonstrating various functionalities, including file system operations, server creation with Express, and streaming data. It showcases how to read and write files, create and delete directories, and set up an HTTP server that serves HTML files based on URL requests. Additionally, it includes examples of using lodash for random number generation and function invocation control.

Uploaded by

h4838543
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

First.

js

setTimeout(()=>{

[Link]('hello node js');

clearInterval(int);

}, 3000)

const int=setInterval(()=>{

[Link](Date(),1000);

},1000);

[Link](__dirname);

[Link](__filename);

[Link]
const fs=require('fs');

’.reading files’
// [Link]('./docs/[Link]',(err,data)=>{

// if(err){

// [Link](err);

// }

// [Link]([Link]());

// });

// //writting file

// [Link]('./docs/[Link]','hello student gain',()=>{

// [Link]('file was written')


// })

// //making file

// [Link]('./assets',(err)=>{

// if(err)

// {

// [Link](err)};

// [Link] ('file created');

// })

// //deleting file

// if(![Link]('./assets')){

// [Link]('./assets',(err)=>{

// if(err)

// {

// [Link](err)};

// [Link] ('file created');

// })

// }

// else {

// [Link]('./assets',(err)=>{

// if(err)

// {
// [Link](err)}

// [Link] ('deleted');

// })

// }

if(![Link]('./assetssss')){

[Link]('./assetssss',(err)=>{

if(err)

[Link](err)};

[Link] ('file created');

});}

else{

[Link]('./assetssss',(err)=>{

if(err)

[Link](err)};

[Link] ('file deleted');

})}
[Link]

const names =['abebe','kebede','tadesse','belay'];

const ages =[23,54,67,89];

[Link]={ names ,ages};

[Link]

//const {names, ages}= require('./name');


//[Link](names,ages);
optionaly
const x= require('./name');
[Link]([Link],[Link])
express
const express=require('express');

const { dirname } = require('path');

//listen

const app=express();

[Link](3000);

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

// [Link]('<h1>home page</h1>')

// });

// [Link]('/about',(req,res)=>{

// [Link]('<h1>About page </h1>')

// })

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

[Link]('./assets/[Link]',{root: __dirname});

});

[Link]('/about',(req,res)=>{

[Link]('./assets/[Link]',{root: __dirname});

});

[Link]('/ about-us',(req,res)=>{

[Link]('./assets/[Link]');

});

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

[Link]('./assets/[Link]',{root:__dirname });});
stream
const fs=require('fs');

const readStream= [Link]('./docs/[Link]',{encoding: 'utf8'});


[Link]('data',(chunk)=>{
[Link]('...chunk new....');
[Link](chunk)

});
//short made
[Link](writeStream)

[Link]
const http=require('http');

const fs=require('fs')

const _ =require('lodash')

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

[Link]([Link],[Link]);

//lodash

const num =_.random(0,20);

[Link](num)

//ones

const great=_.once(()=>{
[Link]('hello');

})

great();

great();

//sending header

[Link]('content-type','text/html');

// [Link]('hello naty this is server creating class');

// [Link]('<h1> hello naty this is server creating class </h1>');

// [Link]('./assets/[Link]',(err,data)=>{

// if(err){

// [Link](err);

// [Link]();}

// else

// {

// [Link](data);

// [Link]();}

// })

let path ='./assets/';

switch([Link]){

case '/':
path +='[Link]';

[Link]=200;

break;

case '/about':

path +='[Link]';

[Link]=200;

break;

case'/about-me':

[Link]=301;

[Link]('location','/about')

[Link]()

default:

path +='[Link]';

[Link]=400;

break;

[Link](path,(err,data)=>{

if(err){

[Link](err);

[Link]();

else{

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

})

});

[Link](4000, 'localhost',()=>{

[Link]('listinning for request on port 4000')

})

You might also like