diff --git a/README.md b/README.md index ac84053..716cf92 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/express-module/Readme.md b/express-module/Readme.md new file mode 100644 index 0000000..99a5790 --- /dev/null +++ b/express-module/Readme.md @@ -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) \ No newline at end of file diff --git a/express-module/code/createExpressApp.js b/express-module/code/createExpressApp.js new file mode 100644 index 0000000..1b68e40 --- /dev/null +++ b/express-module/code/createExpressApp.js @@ -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. diff --git a/express-module/code/deleteApi.js b/express-module/code/deleteApi.js new file mode 100644 index 0000000..6f3183c --- /dev/null +++ b/express-module/code/deleteApi.js @@ -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"); +}) \ No newline at end of file diff --git a/express-module/code/getApi.js b/express-module/code/getApi.js new file mode 100644 index 0000000..bc04c39 --- /dev/null +++ b/express-module/code/getApi.js @@ -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"); +}) \ No newline at end of file diff --git a/express-module/code/postApi.js b/express-module/code/postApi.js new file mode 100644 index 0000000..91a69d1 --- /dev/null +++ b/express-module/code/postApi.js @@ -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"); +}) \ No newline at end of file diff --git a/express-module/code/putApi.js b/express-module/code/putApi.js new file mode 100644 index 0000000..94db9a5 --- /dev/null +++ b/express-module/code/putApi.js @@ -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"); +}) \ No newline at end of file diff --git a/express-module/code/routing.js b/express-module/code/routing.js new file mode 100644 index 0000000..56d1ba7 --- /dev/null +++ b/express-module/code/routing.js @@ -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(''); + +router.get('', reqRouterDest.nameOfFunction); +router.post('', reqRouterDest.nameOfFunction); +router.put('', reqRouterDest.nameOfFunction); +router.delete('', reqRouterDest.nameOfFunction); \ No newline at end of file