My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Node.js as API development : 1

Pratik Pathak's photo
Pratik Pathak
·Apr 16, 2021·

3 min read

I'm Starting with Node.js as a backend API development. Since my personal blog went down and also I don't have money to renew it I'm moving with hash node. There is no particular reason for choosing hashcode for my blog but I like the subdomain provided by the hashcode. Let's hope I don't have to pay here for hosting my blogs and also no third-party ads should get placed on my blogs. I'm going to use it as my note's notebook

Basics of Node.js + Slide are in this repo

What is NodeJs?

Node.js is an environment for running JavaScript outside the browser. Node.js can be used for build tools, desktop apps, mobile apps, databases, and more.

Browser Javascript VS Node.js

image.png

Some Imp Globals

image.png

Modules:

How to create/import a module (Encapsulated Code):

var module1 = (function(exports, require, module, __filename, __dirname){
  // your node js code in a file
})

var module2 = (function(exports, require, module, __filename, __dirname){
  // your node js code in another file
})

Creating Modules

All your Nodejs code are modules. As the author, you decide how and what to expose from your modules to other modules. You do this with the module global object provided to you by the Nodejs runtime

Example for exporting a node module (i.e. code.js):

const add=(num1,num2) => {}
const notPublic = () => {}

module.exports = { add, thing() {}, value:1} #Named export

Here, module.exports is used to export the add, thing, and a value file but notPublic has not been exported since it's not written in in module.exports. There is one more way to export the module which is exports global, it's not much reliable and you cannot explicitly export anything so please refrain from using it.

if you want to export one thing you can do it like

module.exports = add #Default Exports

we can access this using . operation like .add, .value, etc

There should be only one "module.exports". If you write more than one module.exports it will overwrite the existing one. If you, later on, find that you need to export a few more modules and you have already written module.exports then use this method:

module.exports.more = #Names of module,objects,etc

Importing Modules

The Nodejs runtime injects another global, require. This function takes a relative path to the module that you want to consume and synchronously loads it by returning whatever the target module exported.

Modules can be imported using the require() module. for example:

const val = require('./code')

Note: we have to write a relative path

There are two types of modules, the first one is created by us or let's say stored in the '.js' file, and the second one is inbuilt modules of node eg. 'fs'. We have to write relative path with "." in first case

Follow this link for continue reading: