GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - anbuleo/nodejs: session code practice

BranchesTags
Open more actions menu

Latest commit

 

History

1 Commit
1 Commit

Folders and files

NameName
Last commit message
Last commit date
date.txt
date.txt
 
 
index.js
index.js
 
 
readme.txt
readme.txt
 
 
sample.txt
sample.txt
 
 
sampleForWriteFile.txt
sampleForWriteFile.txt
 
 
sampleSync.txt
sampleSync.txt
 
 

Repository files navigation

console.log("welcome world of backend")

const os = require('os')
// console.log(os.cpus())
console.log(os.uptime())

to read a file by using readFile

const { error } = require('console')
const fs = require('fs')

fs.readFile('./sample.txt','utf-8',(error, data)=>{
    if(error){
        console.log(error)
    }
    else{
        console.log(data)
    }
})

Using  readFileSync method // comparing to both this exceute fast because O(n)


let data = fs.readFileSync('./sample.txt','utf-8')
console.log('sync',data)

To write a new file =>  fs.writeFile('path','data','formate(utf-8)',(error)=>{ if (error) console.log(error) else console.log('File created success')})

fs.writeFile('./sampleForWriteFile.txt','Hai we created a new file','utf-8',(error)=>{
    if(error)
    console.log(error)
    else
    console.log('File created Successfully')
})

To write and read a sync method

To write

fs.writeFileSync('./sampleSync.txt','This is a sync method','utf-8')

let syncdata = fs.readFileSync('./sampleSync.txt','utf-8')

To create a server 

const http = require('http')

const server  = http.createServer((req,res)=>{
    res.writeHead(200,{
        "content-type":"text/html"
    })
    res.end(`<h1>Hello Server Is runing</h1>`)
})

server.listen(8000,()=>"server is running in 8000")

TO create a json

const http = require('http')

const server = http.createServer((req,res)=>{
    res.writeHead(200,{
        "content-type":"application/json"
    })
    res.end(JSON.stringify({
        name:"anbu",
        email:"[email protected]"
    }))

})

server.listen(8000,()=>console.log("server is lisning port 8000"))

To write a file and read to listen the port 


const fs = require('fs')
const http = require('http')
let time = new Date().toLocaleString()



const server = http.createServer((req,res)=>{

    try {
        fs.writeFileSync('./date.txt',`${time}`,'utf-8')
       let data =  fs.readFileSync('./date.txt','utf-8')
       res.writeHead(200,{
        "content-type":"text/html"
       
    })
    res.end(`${data}`)
    } catch (error) {
        console.log(error)
    }   

})

server.listen(8000,()=>console.log("server is lisning port 8000"))

About

session code practice

Topics

httpnodejsosportreadfilereadfilesyncservertry-catchutf-8writefilewritefilesync

Resources

Readme
Activity

Stars

0 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.