Menu

Showing posts with label NodeJS. Show all posts
Showing posts with label NodeJS. Show all posts

ReferenceError: fetch is not defined

If you are seeing this error while using the node that means your node version doesnt support out of the box fetch API. There could be two possible solution

1. Upgrade the node version

Check installed node version using command >> node -version

Then using this command update node version to latest node version. npm install -g node

2. Install node-fetch

You could use the fetch with your existing project and node version as well. For that you have to install the node-fetch and use it.


a). Node install command
npm install node-fetch@2

b). After successful install create a fetch constant in your JS file where you are usign the fetch. Like below.

const fetch = require("node-fetch");

Callback function JavaScript and NodeJS

Lets understand the callback fucntion in NodeJS using the below geocode example.

Here we have created geoCode function with two paramters, first parameter will take input of the location name and second parameter the function that needs to call. Similary, we can add or pass any number of paramters in fucntion as per need.

const geoCode = (location, callback) => {
    const data = {
        location: location,
        latitude: 0,
        longitude: 0
    }
    callback(data)
}

 

When we call the geoCode function, we are here passing a city name and second paramter a fucntion itself that print the return data from callback on console.
 
geoCode('New Delhi', (callbackData) => {
    console.log(callbackData)
});

Return

{ location: 'New Delhi', latitude: 0, longitude: 0 }

How to send a html page on a page request on node?

While working with NodeJS server application, we may come to situation where we need to redirect out request to any html page and render the same html page to browser. Below is the sample code to send a html file as a response in NodeJS app. 

app.get('/home', (req, res) => {
    res.sendFile(publicPageDire+'/index.html')
})


In place of publicPageDire variable we could also use path.join() like below


app.get('/home', (req, res) => {
    res.sendFile(path.join(__dirname, '../public/index.html'))
})

Write a file in Node | NodeJS tutorial

In Node using FileSystems, developer can perform the various file operations, create file, add content into file, delete file.

First import the FileSystems fs using the below node code

const fs = require('fs')


Then, using the Node FIleSystems object fs we could perform the form operations. Liek below.

fs.writeFileSync('notes.txt', 'This file is created by NodeJS application. ')


writeFileSync will create and write the first data into the file. writeFileSynch method have two parameters, first file name and second data.


Next if we want to add or append data into an existing file then use appendFileSync method. Like Below.

fs.appendFileSync('notes.txt', 'DO NOT DELETE THIS FILE')


Hope this help you to understand the basic of file systems in NodeJS.