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 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')); | |
}); |
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 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: |
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 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')) | |
} | |
} | |
} |
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
app.post("/api/myform/:categoryId", function(req, res) { | |
res.status(200) | |
.send({queryParamValues: req.query.create + ' ' + req.query.notify}); | |
}); |
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
app.post("/api/myform/:categoryId", function(req, res) { | |
res.status(200) | |
.send({result: 'Category you sent is' + req.params.categoryId}); | |
}); |
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
/* 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 */}); |
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
app.get('/', function(req, res) { | |
res.status(200).send({status: "running", time: Date.now()}); | |
}); |
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'); | |
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") |
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
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 | |
}) | |
} |
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
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}); | |
}); |