Skip to content

Instantly share code, notes, and snippets.

View rasolofonirina's full-sized avatar

Eric Rasolofonirina rasolofonirina

  • Madagascar
  • 17:37 (UTC +03:00)
View GitHub Profile
@rasolofonirina
rasolofonirina / hello-world.js
Last active March 25, 2022 12:45
Hello world with Node.js
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')
})
@rasolofonirina
rasolofonirina / blocking-vs-non-blocking.js
Last active March 25, 2022 12:45
blocking vs non-blocking
// 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)
@rasolofonirina
rasolofonirina / mixing-and-non-blocking-code.js
Last active March 25, 2022 12:45
Dangers of mixing blocking and non-blocking code
// 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')
@rasolofonirina
rasolofonirina / basic-server.js
Last active March 25, 2022 12:44
Basic server
const http = require('http')
const server = http.createServer((req, res) => {
res.writeHead(200)
res.end('Hello everybody !')
})
server.listen(8080)
@rasolofonirina
rasolofonirina / basic-server-with-html.js
Last active March 25, 2022 12:43
Basic server with HTML
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)
@rasolofonirina
rasolofonirina / url.js
Last active March 25, 2022 12:35
Test URL
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()
})
@rasolofonirina
rasolofonirina / url-with-content.js
Last active March 25, 2022 12:36
URL with content
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')
}
@rasolofonirina
rasolofonirina / url-with-settings.js
Last active March 25, 2022 12:38
URL with settings
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'])
}
@rasolofonirina
rasolofonirina / event.js
Last active March 25, 2022 12:39
Event Node.js
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) => {
@rasolofonirina
rasolofonirina / emit-event.js
Last active March 25, 2022 12:40
Emit event
const EventEmitter = require('events').EventEmitter
const zavatra = new EventEmitter()
zavatra.on('velona', (message) => {
console.log(message)
})
zavatra.emit('velona', 'Io fa velona !')