Node Notes
Node Notes
Node Notes
What is NodeJS?
NodeJS is an environment to run JavaScript outside the browser
It was created using Chrome V8 JavaScript engine
It was created by Ryan Dahl, who took chrome V8 engine and built NodeJS on
top of it.
Note:
There is no window(js) object in nodeJS
NodeJS has built-in node modules(packages) that we can use out of the box without
installing them.
there are many modules in node some of them are given below.
OS module:
it has lot of useful methods and properties to know about user’s pc details and much
more
path module:
it is used to create a path for a specific file or directory because not all computers use
same path model. For example
in windows: path is denoted in documents\projects\ (in backward slash)
in mac : path is denoted in forward slash documents/projects/
so we can use this path module to make node automatically create path according to
the user’s pc.
fs module
file system module is used to access file, create , edit, modify a file
const { readFile, writeFile } = require("fs");
const file = readFile("./users.txt", "utf8", (err, result) => {
if (err) {
console.log(err);
}
const name = result;
writeFile("./hello.txt", `hey there ${name}`, (err, result) => {
if (err) {
console.log(err);
}
});
});
This is not the right asynchronous approach because it has a callback hell. The better
approach will be given later.
npm i nodemon
“script” : {
“dev” : “nodemon app.js”
}
or
“script” : {
“start” : “nodemon app.js”
}
dependencies: {
“bootstrap” : “^6.2.5”
}
or
dependencies: {
“bootstrap” : “~6.2.5”
}
Versioning 6.2.5
6 is major upgrade which means functionality will broke(may) when you update the
older project to a major version of a package
the way event works in node is We listen for an event and send our response to that
event
in the above example we are importing the events module which is built-in node
package, since it is a class we are creating a instance of a class.
then on is used to listen for an event. response is the event you are listening for .
followed by a callback function to respond to that event.
for all the events we have to emit that event. you will see more details about events
later
Note:
you should always emit the event or else it wont work
Streams in Node JS
Streams is used to read/write file in chunks. If a file is very big in size, you want to
read that file the browser will take more load time to read that file. So what stream
will do is it will send the data in chunks. if a text file is 1MB it will send the data in
chunks i.e 500kb , 500kb
Express JS
Middleware:
Middleware is a function in express. when a user sents a request
req -> MiddlewareFunction -> res (controller)
This middleware function has access to res, res, next object. This is very powerful
because We can directly send a response without the need to contact the controller
if a user sents a request.
Eg:
you can setup a middleware to check if a user is logged in or not when a request
comes from the frontend with the middleware function. If the user logged in then
you can go ahead to call the controllers, if the user has not logged in you can send a
response in the middleware itself.
Note:
You should always invoke the next function in the middleware if you are not sending
any response. the next function is used to call the controller if you don’t invoke it you
get an error. But if you are sending a response in the middleware no need to invoke
the next function.
you can use multiple middleware functions for a single/any route, just add those
functions in an array and all those middleware functions have access to req, res, next
There are two ways to do so
using app.use
app.use(‘/api/v1/products’, [logger, authorize])
or
Note:
These middleware functions in the array execute in order keep that in mind
app.use()
It is used to listen for request from the application for every request. Put the
middleware functions in app.use, serve static files whatever you do it will execute for
every single request if a path is not defined.
Eg:
app.use(express.json())
app.use(‘/products’, logger)
Now app.use only listens to the route mentioned here and also listens the child route
of /products too. (i.e) products/items
Note:
app.use() always execute in order only make sure that you always always put
app.use() in top of all file and make sure the middlewares are executed in app.use
are in correct order.