Skip to content

Instantly share code, notes, and snippets.

View claytonbez's full-sized avatar

Clayton Bezuidenhout claytonbez

  • Bestseller BV
  • The Netherlands
View GitHub Profile
const express = require('express');
const app = express();
//acts as a middleware
//to handle CORS Errors
app.use((req, res, next) => { //doesn't send response just adjusts it
res.header("Access-Control-Allow-Origin", "*") //* to give access to any origin
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization" //to give access to all the headers provided
@claytonbez
claytonbez / http-proxy.js
Last active August 3, 2018 08:43
Express JS proxy script with client example
//SERVER SCRIPT
var express = require('express');
var app = express();
var request = require('request');
app.get('/:forwardParams',function(req,res){
request.get('http://www.google.com/' + req.params.forwardParams, function (err, response) {
if(!err){
res.send(response.body);
}
else{
@claytonbez
claytonbez / client.js
Created July 30, 2018 13:46
socket-io-client starter
var serverIndex = args.indexOf('--server') + 1;
if (!serverIndex) {
console.log('No port specified. use --port <portnumber> as arguments when starting this script'.red.bold);
process.exit();
}
var server = args[serverIndex];
var io = require('socket.io-client');
var socket = io.connect(server);
socket.on('connect',function(){
@claytonbez
claytonbez / index.html
Created July 30, 2018 12:41 — forked from diogocapela/index.html
Starter Template CDN Template Bootstrap 4
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Starter Template</title>
<meta name="description" content="">
@claytonbez
claytonbez / request-file-pipe.js
Created July 30, 2018 12:22
Using the request module to download a file.
var http = require('http');
var fs = require('fs');
var file = fs.createWriteStream("file.jpg");
var request = http.get("http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg", function(response) {
response.pipe(file);
});
@claytonbez
claytonbez / readline-example.js
Created July 30, 2018 12:16
Starter for NodeJS readline
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', (input) => {
console.log(`Received: ${input}`);
});
@claytonbez
claytonbez / SerialPortEventHandler.js
Last active July 4, 2018 07:00
NodeJS Serial Port Handler
//--------------------------------------------------------------------------------
// SERIAL PORT HANDLER
// This module can be imported into nodejs using require method.
// It uses .start(device,baudrate); to create aconnection to the serial hardware
// After that, it manager disconnects to the hardware and retries.
// It employs an array buffer to slow down the processing of incoming commands
// to a speed that your program can handle
//--------------------------------------------------------------------------------
var EventEmitter = require('events').EventEmitter;
var util = require('util');
@claytonbez
claytonbez / Socket.io-client NODEJS
Created July 2, 2018 16:28
Socket.io Client example for NodeJS
var io = require('socket.io-client');
var connectData = { server:"http://127.0.0.1:80"}
var socket = io.connect(connectData.server);
socket.on('connect',function(){
console.log('Connected to main Server');
});
socket.on('disconnect',function(){
console.log('Connected to main Server');
@claytonbez
claytonbez / Socket.io-server
Last active July 2, 2018 16:27
Socket.io Server example
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io')(server);
app.use("/", express.static(__dirname + "/public"));
app.get("/", function (req, res) {
res.sendFile(__dirname + '/index.html');
});