Skip to content

Instantly share code, notes, and snippets.

View rasolofonirina's full-sized avatar

Eric Rasolofonirina rasolofonirina

  • Madagascar
  • 15:35 (UTC +03:00)
View GitHub Profile
@rasolofonirina
rasolofonirina / uid.js
Created March 23, 2022 14:04
Generate Unique ID
function uid() {
const head = Date.now().toString(36)
const tail = Math.random().toString(36).substring(2)
return head + tail
}
uid()
@rasolofonirina
rasolofonirina / first-improvement.js
Last active March 25, 2022 12:42
First improvement
// Replace the line 252 to 316 by this
if (messageText) {
const greeting = new RegExp(/^(kaiz|kez|slt|salut|bjr|bonjour|hi|hello|hey|hola)/i)
if (messageText.match(greeting)) {
sayHello(senderID)
} else if (messageText.match('image')) {
sendImageMessage(senderID)
} else if (messageText.match('gif')) {
@rasolofonirina
rasolofonirina / export.js
Last active March 25, 2022 12:41
Export Node.js
// Create a folder mymodule and put this in file named mymodule.js
const kaizKaiz = () => {
console.log('Kaiza kaiza daholo e !')
}
const surSur = function() {
console.log('Sur sur e !')
}
exports.kaizKaiz = kaizKaiz
@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 !')
@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 / 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 / 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.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 / 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 / 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)