Skip to content

Instantly share code, notes, and snippets.

View RayanBassetti's full-sized avatar
🎯
Focusing

Rayan Bassetti RayanBassetti

🎯
Focusing
  • Paris
View GitHub Profile
@RayanBassetti
RayanBassetti / cors.js
Created January 26, 2020 12:33
Middleware to prevent cors errors (Express.js)
// middleware that ensures that cors errors are prevented
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*'); // allow the access of the api, to all clients/websites asking
res.header('Access-Control-Allow-Headers', '*'); // which kind of header we want to accept
if(req.method === 'OPTIONS') {
res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE');
return res.status(200).json({});
}
next();
})
@RayanBassetti
RayanBassetti / app.js
Created July 4, 2019 09:17
Odin - NodeJS Project #1
const http = require('http');
const fs = require('fs');
const url = require('url');
http.createServer(function(req, res) {
var q = url.parse(req.url, true);
var filename = "." + q.pathname;
if(filename == "./") {
fs.readFile('index.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});