Created
December 1, 2018 16:43
-
-
Save pedrouid/6c95e23e4436c215d392d5110bf18fb7 to your computer and use it in GitHub Desktop.
Node.js Hello World Server Example (http vs express.js vs fastify.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
| const express = require('express'); | |
| const app = express(); | |
| // Routes | |
| app.get('/', (req, res) => { | |
| res.send('Hello World!'); | |
| }); | |
| // Listen | |
| const port = process.env.PORT || 5000; | |
| app.listen(port, () => { | |
| console.log(`Listening on localhost:${port}`); | |
| }); |
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
| const fastify = require('fastify'); | |
| const app = fastify({ logger: true }); | |
| // Routes | |
| app.get('/', (req, res) => { | |
| res.send('Hello World!'); | |
| }); | |
| // Listen | |
| const port = process.env.PORT || 3000; | |
| app.listen(port, (err, address) => { | |
| console.log(`Server listening on: ${address}`); | |
| }); |
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
| const http = require('http'); | |
| const port = process.env.PORT || 3000; | |
| http | |
| .createServer((req, res) => { | |
| res.write('Hello World!'); | |
| res.end(); | |
| }) | |
| .listen(5000); | |
| console.log(`Server listening on port ${port}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment