What is node and npm : Blog
- Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine.
- Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.
- Npm is the package mangaer for node.js you can install any package on your machine from the repository.
- npx is node package execute, you can execute
- All npm packages contain a file, usually in the project root, called package.json - this file holds various metadata relevant to the project.
- This file is used to give information to npm that allows it to identify the project as well as handle the project's dependencies.
- It can also contain other metadata such as a project description, the version of the project, even configuration data - all of which can be vital to both npm and to the end users of the package.
- The
package.json
file is normally located at the root directory of a Node.js project.
- Initialize a node application
npm init
-
You will find a menu Most of these details are pretty obvious, and for many of the prompts you can just select the defaults.
-
Entry point is the javascript file that will be invoked when consumers of your module “require” it, this file will include the main logic for your module.
-
After filling in the required information, a
package.json
file will be created with all the entered details -
Read about content inside package.json in detail in this blog: Blog
- Install required dependencies
npm install express --save
- This shall create a directory
node_modules
in your working directory.
-
Create a file
app.js
or name it as your entry point. -
Start the server
//Requiring the express module that you installed
const express= require('express');
const app=express();
//Starting the server at PORT 3000
app.listen(3000, function(){
console.log("Server Has Started");
});
- At the software level, within an operating system, a port is a logical construct that identifies a specific process or a type of network service.
- A port is identified for each transport protocol and address combination by a 16-bit unsigned number, known as the port number.
- You can read them about in depth here
- Used to retrieve information
- Data is sent via query string
- Info is plainly visible in the URL
- Limited amount of data can be sent
- Used to post data to the server
- Used to write/create/ update
- data is sent via request body not by query string
- Send any type of data
- These two requests are used to update a resource on a server.
- Data is sent via request body
- The main difference between the
PUT
andPATCH
method is that thePUT
method uses the request URI to supply a modified version of the requested resource which replaces the original version of the resource, whereas thePATCH
method supplies a set of instructions to modify the resource.
- This request is used to delete a resource from a server.
Stand for create read, update and destroy.
- Create a 'root' route
//Requiring the express module that you installed
const express= require('express');
const app=express();
//Creating making app to make a get request at route '/' (i.e localhost:xxxx/), function takes two arguments request (req) and response (res), we are returning a response saying hello world.
app.get("/", (req, res)=>{
res.send("Hello World");
});
//Starting the server at PORT 3000
app.listen(3000, function(){
console.log("Server Has Started");
});