Created
October 20, 2020 09:26
-
-
Save hiteshsahu/0ebcc4067875fc58f827d0d9e5f9995d to your computer and use it in GitHub Desktop.
Using ES6 in Node JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import express from "express" | |
const bodyParser = require('body-parser'); | |
/** | |
* Example of using ES6 syntectic sugar to create Express JS server | |
*/ | |
class ExpressServer { | |
constructor(hostname =process.env.LOCAL_HOST, port= process.env.DEFAULT_PORT) { | |
this.serverName = 'Express Server'; | |
this.hostname = hostname; | |
this.port = port; | |
//Auto Start Server | |
this.initServer() | |
} | |
initServer=()=> { | |
//Create Server | |
this.server = express() | |
this.server.use(bodyParser.json()); // for parsing application/json | |
this.server.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded | |
this.server.get('/user', (req, res)=> { | |
res.send('Got a GET request at /user') | |
next() | |
}) | |
this.server.put('/user', (req, res)=> { | |
res.send('Got a PUT request at /user') | |
next() | |
}) | |
this.server.delete('/user', (req, res)=> { | |
res.send('Got a DELETE request at /user') | |
next() | |
}) | |
this.server.post('/', (req, res, next) => { | |
console.log(req); | |
next() | |
}); | |
this.server.get('/', (req, res)=> { | |
res.send('Hello World!') | |
next() | |
}) | |
//Start Listening | |
this.server.listen(this.port, () => { | |
console.log(`${this.serverName} Started at http://${this.hostname}:${this.port}/`); | |
}) | |
} | |
} | |
//EXPORT MODULE | |
module.exports = ExpressServer | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import NodeServer from "./server/NodeServer" | |
import ExpressServer from "./server/ExpressServer" | |
import dotenv from "dotenv" | |
dotenv.config() | |
const WebServer = new ExpressServer(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http from "http" | |
/** | |
* Example of using ES6 syntectic sugar to create Node JS server | |
*/ | |
class NodeServer { | |
constructor(hostname =process.env.LOCAL_HOST, port= process.env.DEFAULT_PORT) { | |
this.serverName = 'Node Server'; | |
this.hostname = hostname; | |
this.port = port; | |
//Auto Start Server | |
this.initServer() | |
} | |
initServer=()=> { | |
//Create Server | |
this.server = http.createServer((req, res) => { | |
res.statusCode = 200; | |
res.setHeader('Content-Type', 'text/plain'); | |
res.end(this.sayHello()); | |
}); | |
//Start Listening | |
this.server.listen(this.port, this.hostname, () => { | |
console.log(`${this.serverName} Started at http://${this.hostname}:${this.port}/`); | |
}); | |
} | |
sayHello=()=> { | |
return ` Hello World from ${this.serverName} Running at http://${this.hostname}:${this.port}/`; | |
} | |
} | |
//EXPORT MODULE | |
module.exports = NodeServer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment