Skip to content

Instantly share code, notes, and snippets.

View ganeshkbhat's full-sized avatar

Krishnamurthy G B ganeshkbhat

View GitHub Profile
@ganeshkbhat
ganeshkbhat / expressjs-serving-static-file.snippet.js
Last active October 4, 2018 05:48
ExpressJS Series: Serving static ffile
const path = require('path');
/* ALL OTHER CODE */
// Serves index.html from the public folder when ...
// ... the client requests http://127.0.0.1:9001/
app.get("/", function(req, res) {
res.status(200)
.sendFile(path.join(__dirname, 'public', 'index.html'));
});
@ganeshkbhat
ganeshkbhat / expressjs-static-folder-snippet.js
Last active October 4, 2018 05:43
ExpressJS Series: Serving static folder
// public folder resides in our project folder 'basicsrv'
// basicsrv/public
// Any files within public - like html, css, js, images will be served as follows:
// http://127.0.0.1:9001/public/styles.css
app.use(express.static('public'));
// RELATIVE PATH USAGE ASSIGNED DIFFERENT EXTERNAL FOLDER NAME
// basicsrv/client/assets will be served as http://127.0.0.1:9001/assets
// Any assets like html, css, js, images will be served as follows:
@ganeshkbhat
ganeshkbhat / expressjs-security-cors-origin-snippet.js
Created October 4, 2018 03:32
ExpressJS - Server Security with Cors Origin
const whitelist = ['http://example1.com', 'http://example2.com'];
const corsOptions = {
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true)
} else {
callback(new Error('Not allowed by CORS'))
}
}
}
@ganeshkbhat
ganeshkbhat / expresjs-routes-four-snippet.js
Created October 4, 2018 03:19
ExpressJS Series: Creating the server routes - 4
app.post("/api/myform/:categoryId", function(req, res) {
res.status(200)
.send({queryParamValues: req.query.create + ' ' + req.query.notify});
});
@ganeshkbhat
ganeshkbhat / expressjs-routes-three-snippet.js
Created October 4, 2018 03:19
ExpressJS Series: Creating the server routes - 3
app.post("/api/myform/:categoryId", function(req, res) {
res.status(200)
.send({result: 'Category you sent is' + req.params.categoryId});
});
@ganeshkbhat
ganeshkbhat / expressjs-routes-two-index.js
Created October 4, 2018 03:17
ExpressJS Series: Creating the server routes - 2
/* POST */
app.post("/", function(req, res) { /* Logic and response comes here */});
/* PUT */
app.put("/", function(req, res) { /* Logic and response comes here */});
/* PATCH */
app.patch("/", function(req, res) { /* Logic and response comes here */});
/* DELETE */
app.delete("/", function(req, res) { /* Logic and response comes here */});
/* OPTIONS */
app.options("/", function(req, res) { /* Logic and response comes here */});
@ganeshkbhat
ganeshkbhat / expressjs-routes-one-index.js
Created October 4, 2018 03:16
ExpressJS Series: Creating the server routes - 1
app.get('/', function(req, res) {
res.status(200).send({status: "running", time: Date.now()});
});
@ganeshkbhat
ganeshkbhat / expressjs-basic-server-index.js
Last active October 4, 2018 03:15
ExpressJS Series: Creating the basic server
const express = require('express');
let app = express();
app.get('/', function(req, res) {
res.status(200)
.send({status: "running", time: Date.now()})
});
const host = "127.0.0.1";
const port = 9001;
app.listen(host, port, function() {
console.log("Server started")
@ganeshkbhat
ganeshkbhat / expressjs-req.range-snippet.js
Created October 4, 2018 03:11
ExpressJS Series: Usage req.range
app.post("/someUrl", function(req, res) {
// parse range Header from request
let range = req.range(1000);
// the type of the range
if (range.type === 'bytes') {
// the ranges
range.forEach(function (r) {
// do something with r.start and r.end
})
}
@ganeshkbhat
ganeshkbhat / expressjs-req.is-snippet.js
Created October 4, 2018 03:10
ExpressJS Series: Usage req.is
app.post("/someUrl", function(req, res) {
// Content-Type => "application/json" (Header Content-Type in request)
let getContent = req.is('application/json');
if (!getContent) {
res.status(406).send("Not Acceptable");
}
// Raw buffer capture in request body (will be covered later)
const contentBody = JSON.parse(req.body.toString('utf8'));
res.send(200).send({result:"Success", content: contentBody});
});