const http = require("http");
const server = http.createServer((req,resp)=>{
resp.writeHead(200,{"Content-Type":"text/plain"});
resp.end("Hello Ajay Kumar")
})
const PORT = process.env.PORT || 5000;
server.listen(PORT,()=>{
console.log(`my server start on this port ${PORT}`)
});
Feature | Express.js | Node.js |
---|---|---|
Type | Framework | Runtime Environemnt |
Language | JavaScript | JavaScript |
Purpose | To build web applications | To run JavaScript code outside of a browser |
Features | Routing, middleware, template engines, etc. | Event-driven, non-blocking I/O, etc. |
Dependencies | Node.js | N/A |
Popularity | Very popular | Very popular |
Examples | GitHub, Uber, Netflix | PayPal, LinkedIn, Walmart |
npm init
This will ask you for few configurations about your project you can fill them accordingly, also you can change it later from the package.json file.
npm install express
c) Set the server to listen to port 8000
const express = require('express');
const app = express();
const PORT = 8000;
app.listen(PORT, (error) =>{
if(!error)
console.log(`My server start on this port ${PORT}`)
else
console.log(" Server can't start", error);
}
);
d) Step to run the application :
node index.js
-save | -save-dev |
---|---|
The package installed is core dependency. | The package installed is not a core rather development dependency. |
All core dependency is listed under dependencies in package.json. | All development dependency is listed under devDependencies in package.json. |
It will be installed if a third person tries to install or clone your package. | It will be installed if a third person tries to clone your package. |
Example: express, body-parser etc. | Example: nodemon |
Synchronous function | Asynchronous function |
---|---|
These are the function that block the execution of the program whenever an operation is performed. Hence these are also called blocking operations. | These are the operations that do not block the execution of the program and each command is executed after the previous command even if the previous command has not computed the result. |
We use these functions to perform lightweight tasks | We use these functions to perform heavy tasks. |
// for Single line comments and
/* Multi
Line
Comment
*/
(function(){
let x="Ajay"
console.log(x) // Ajay
})();
Example 2 :
(function(a,b){
console.log(a+b) // 30
return a + b;
})(10,20);
Example 3 :
(function() {
var counter = 0;
function add(a, b) {
return a + b;
}
console.log(add(10,20)); // 30
}());
Example 4 :
(function() {
var counter = 0;
function add(a, b) {
return a + b;
}
console.log(add(10,20)); // 30
}());
Important Point
function myFunc()
{
console.log("Welcome to"); // Welcome to
// This will be executed after executing the previous log.
(function() {
console.log("Ajay Kumar"); // Ajay Kumar
})();
console.log("Hi There!"); // Hi There!
}
myFunc();
// Declaring the parameter required.
(function(dt) {
console.log(dt.toLocaleTimeString()); // 6:12:57 PM
// Passing the Parameter.
})(new Date());