This file contains 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 hostname = '127.0.0.1' | |
const port = 3000 | |
const server = http.createServer((req, res) => { | |
res.statusCode = 200 | |
res.setHeader('Content-Type', 'text/plain') | |
res.end('Hello World') | |
}) |
This file contains 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
// Blocking | |
const fs = require('fs') | |
const data = fs.readFileSync('/file.txt') | |
console.log(data) | |
// Non-blocking | |
const fs = require('fs') | |
fs.readFile('/file.txt', (err, data) => { | |
if (err) throw err | |
console.log(data) |
This file contains 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
// Mix | |
const fs = require('fs') | |
fs.readFile('/file.txt', (err, data) => { | |
if (err) throw err | |
console.log(data) | |
}) | |
fs.unlinkSync('/file.txt') | |
// Better way | |
const fs = require('fs') |
This file contains 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 server = http.createServer((req, res) => { | |
res.writeHead(200) | |
res.end('Hello everybody !') | |
}) | |
server.listen(8080) |
This file contains 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 server = http.createServer((req, res) => { | |
res.writeHead(200, {"Content-Type": "text/html"}) | |
res.end('<h1>Hi everybody !</h1>') | |
}) | |
server.listen(8080) |
This file contains 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 url = require('url') | |
const server = http.createServer((req, res) => { | |
const page = url.parse(req.url).pathname | |
console.log(page) | |
res.writeHead(200, {"Content-Type": "text/plain"}) | |
res.write('Hello everybody !') | |
res.end() | |
}) |
This file contains 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 url = require('url') | |
const server = http.createServer((req, res) => { | |
const page = url.parse(req.url).pathname | |
console.log(page) | |
res.writeHead(200, {"Content-Type": "text/plain"}) | |
if (page == '/') { | |
res.write('Home') | |
} |
This file contains 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 url = require('url') | |
const querystring = require('querystring') | |
const server = http.createServer((req, res) => { | |
const settings = querystring.parse(url.parse(req.url).query) | |
res.writeHead(200, {"Content-Type": "text/plain"}) | |
if ('firstname' in settings && 'name' in settings) { | |
res.write('Your name: ' + settings['firstname'] + ' ' + settings['name']) | |
} |
This file contains 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 server = http.createServer((req, res) => { | |
res.writeHead(200) | |
res.end('Hi everybody !') | |
}) | |
/* | |
const server = http.createServer() | |
server.on('request', (req, res) => { |
This file contains 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 EventEmitter = require('events').EventEmitter | |
const zavatra = new EventEmitter() | |
zavatra.on('velona', (message) => { | |
console.log(message) | |
}) | |
zavatra.emit('velona', 'Io fa velona !') |
OlderNewer