Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ Feel free to contribute.
- [Node.js Sequelize Module Code Example](./sequelize-module)
- [Node.js Console Module Code Example](./console-module)
- [Node.js Errors Module Code Example](./errors-module)
- [Node.js Express Module Code Example](./express-module)
24 changes: 24 additions & 0 deletions express-module/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## express

express is a web framework in Node.js which helps providing easy server creation, routing and adding middleware capabilities.

## How to install sequelize and RDBMS associated driver

To install the express dependency run the following command:
npm i express


## Node.js express module code examples

Find [example code](./code/createExpressApp.js) for most creating an express application.

## ToC

<= [Parent](./Readme.md)

- [How to create an express application](./code/createExpressApp.js))
- [How to create get API](./code/getApi.js)
- [How to create post API](./code/postApi.js)
- [How to create put API](./code/putApi.js)
- [How to create delete API](./code/deleteApi.js)
- [How to create a route the request using express](./code/routing.js)
7 changes: 7 additions & 0 deletions express-module/code/createExpressApp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* @description : provided the way to create an express application
*/

let express = require('express'); //This returns a function which is stored in express variable

let app = express(); //Here we are creating the express application, which will be having get, post, etc functionalities.
18 changes: 18 additions & 0 deletions express-module/code/deleteApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* @description : provided the way to create an delete api.
*/

let express = require('express'); //This returns a function which is stored in express variable

let app = express(); //Here we are creating the express application, which will be having get, post, etc functionalities.

//Get API is used to insert values and cannot be hit from browsers. To hit post api's we require special softwares like POSTMAN, curl etc.
app.delete('/', (req, res)=>{
res.send("Correctly hitting the get delete API");
})


//Here the server is started on PORT defined in process.env file, if it doesn't exist it runs on port 8080
app.listen(process.env.PORT || 8080, ()=>{
console.log("Server started successfully");
})
18 changes: 18 additions & 0 deletions express-module/code/getApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* @description : provided the way to create an get api.
*/

let express = require('express'); //This returns a function which is stored in express variable

let app = express(); //Here we are creating the express application, which will be having get, post, etc functionalities.

//Get API is used to retreive values and can be hit from browsers or softwares like POSTMAN etc.
app.get('/', (req, res)=>{
res.send("Correctly hitting the get API");
})


//Here the server is started on PORT defined in process.env file, if it doesn't exist it runs on port 8080
app.listen(process.env.PORT || 8080, ()=>{
console.log("Server started successfully");
})
18 changes: 18 additions & 0 deletions express-module/code/postApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* @description : provided the way to create an post api.
*/

let express = require('express'); //This returns a function which is stored in express variable

let app = express(); //Here we are creating the express application, which will be having get, post, etc functionalities.

//Get API is used to insert values and cannot be hit from browsers. To hit post api's we require special softwares like POSTMAN, curl etc.
app.post('/', (req, res)=>{
res.send("Correctly hitting the get POST API");
})


//Here the server is started on PORT defined in process.env file, if it doesn't exist it runs on port 8080
app.listen(process.env.PORT || 8080, ()=>{
console.log("Server started successfully");
})
18 changes: 18 additions & 0 deletions express-module/code/putApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* @description : provided the way to create an put api.
*/

let express = require('express'); //This returns a function which is stored in express variable

let app = express(); //Here we are creating the express application, which will be having get, post, etc functionalities.

//Get API is used to update values already present in database and cannot be hit from browsers. To hit post api's we require special softwares like POSTMAN, curl etc.
app.put('/', (req, res)=>{
res.send("Correctly hitting the get PUT API");
})


//Here the server is started on PORT defined in process.env file, if it doesn't exist it runs on port 8080
app.listen(process.env.PORT || 8080, ()=>{
console.log("Server started successfully");
})
19 changes: 19 additions & 0 deletions express-module/code/routing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* @description : Routing in Express
* Routing is very crucial. It defines the URL structure that can be used to
* interact with the web application.
* Express apps use routers that are containers for a bunch of middleware. The
* middleware holder can be used on a certain route, which enables to place the logic
* in separate files and bring them together.
*/

const express = require('express')

let router = express.Router();

const reqRouterDest = require('<Provide here the path to the controller where we need to route the request to>');

router.get('<Give here the end points we need to redirect>', reqRouterDest.nameOfFunction);
router.post('<Give here the end points we need to redirect>', reqRouterDest.nameOfFunction);
router.put('<Give here the end points we need to redirect>', reqRouterDest.nameOfFunction);
router.delete('<Give here the end points we need to redirect>', reqRouterDest.nameOfFunction);