Skip to content

Instantly share code, notes, and snippets.

View paulobunga's full-sized avatar
🏠
Available for work

Paul Obunga paulobunga

🏠
Available for work
View GitHub Profile
@paulobunga
paulobunga / proxy.js
Created September 9, 2018 12:28 — forked from cmawhorter/proxy.js
Node script to forward all http requests to another server and return the response with an access-control-allow-origin header. Follows redirects.
// Simple proxy/forwarding server for when you don't want to have to add CORS during development.
// Usage: node proxy.js
// Open browser and navigate to http://localhost:9100/[url]
// Example: http://localhost:9100/http://www.google.com
// This is *NOT* for anything outside local development. It has zero error handling among other glaring problems.
// This started as code I grabbed from this SO question: http://stackoverflow.com/a/13472952/670023
@paulobunga
paulobunga / The Modal Code
Created October 30, 2018 20:26
Copy and paste
public function clock_user($fingerprint, $stimestamp, $facilityId) {
$query = $this->db->get_where('fingerprints', array('fingerprint' => $fingerprint, 'facilityId'=> $facilityId));
$result = $query->row();
if($query->num_rows() > 0) {
// return $result->ihris_pid;
$timestamp = strtotime($stimestamp);
@paulobunga
paulobunga / #01 Enter commands in terminal
Last active January 19, 2019 11:48
express_app_getting_started
// Create our default app file
touch app.js
// This will generate a package.json file inside our directory will all the default options set.
npm init -y
// This will install express and express-handlebars
npm install --save express express-handlebars
@paulobunga
paulobunga / #02 Create Our App file
Last active January 19, 2019 12:02
Create the Express App
const express = require('express');
const handlebars = require('express-handlebars');
const port = PROCESS.ENV.PORT || 8080;
const app = express();
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
//Step 1 :: Create working directory
mkdir node_rest_api && cd node_rest_api
//Step 2 :: Create required directories
mkdir models controllers routes common
//Step 3 :: Create the files for out app
touch app.js common/database.js models/user-model.js controllers/user-controller.js routes/user-routes.js
//Step 4 :: Initialise our npm project (with default values)
@paulobunga
paulobunga / app.js
Last active January 28, 2019 12:28
//This is the file using the middleware below
const express = require('express');
const myMiddleware = require('./myMiddleware');
const app = express();
app.get('/', myMiddleware.logToConsole, (req, res) => {
res.send('Middleware example');
});
//Simple middleware which logs to console
var logToConsole = (req, res, next) => {
console.log('I am your Middleware');
next();
}
exports.logToConsole;
@paulobunga
paulobunga / app.js
Created January 28, 2019 12:43
NodeJS Restful API
const express = require('express');
const userRoutes = require('./routes/user-routes');
const app = express();
app.use('/', userRoutes);
app.listen(3000, () => { console.log('App is running on port 3000') });
const express = require('express');
const router = express.Router();
const authMiddleware = require('../middleware/auth-middleware');
const userController = require('../controllers/user-controller');
//Get a list of all the users
//We are using the authentication middleware to protect this route from unauthorised access
router.get('/', authMiddleware.isAuthenticated, userController.getUsers);
@paulobunga
paulobunga / auth-middleware.js
Last active January 28, 2019 13:18
Auth middleware, checks if user is logged in or not
const jwt = require('jsonwebtoken');
exports.isAuthenticated = (req, res, next) => {
//Get the request headers and check if we have an authorization header with our token
if (req.headers && req.headers.authorization && req.headers.authorization.split(' ')[0] === 'JWT') {
//Check the token for validity
var token = req.headers.authorization.split(' ')[1];
jwt.verify(token, 'our-jwt-secret-goes-here', (err, payload) => {