This file contains hidden or 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
// 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 |
This file contains hidden or 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
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); |
This file contains hidden or 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
// 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 | |
This file contains hidden or 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 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'); |
This file contains hidden or 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
//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) |
This file contains hidden or 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
//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'); | |
}); |
This file contains hidden or 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
//Simple middleware which logs to console | |
var logToConsole = (req, res, next) => { | |
console.log('I am your Middleware'); | |
next(); | |
} | |
exports.logToConsole; |
This file contains hidden or 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 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') }); |
This file contains hidden or 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 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); |
This file contains hidden or 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 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) => { |