Created
December 17, 2016 17:27
-
-
Save jacksonfdam/4ae03b0b1ae45ff7fdd02c1ccdb36251 to your computer and use it in GitHub Desktop.
API Server + SPA Static Server
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
var http = require('http'); | |
var fs = require('fs'); | |
var server = http.createServer(function(request, response) { | |
fs.readFile(__dirname + '/index.html', function(err, html) { | |
response.writeHeader(200, { | |
'Content-Type': 'text/html' | |
}); | |
response.write(html); | |
response.end(); | |
}); | |
}); | |
server.listen(8080, function() { | |
console.log('Executando Servidor HTTP'); | |
}); |
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.js | |
var http = require('http'); | |
var fs = require('fs'); | |
var server = http.createServer(function(request, response) { | |
fs.readFile(__dirname + '/estados.json', function(err, arquivo) { | |
var headers = {}; | |
headers["Access-Control-Allow-Origin"] = "*"; | |
headers["Access-Control-Allow-Methods"] = "POST, GET, PUT, DELETE, OPTIONS"; | |
headers["Access-Control-Allow-Credentials"] = true; | |
headers["Access-Control-Max-Age"] = '86400'; // 24 hours | |
headers["Access-Control-Allow-Headers"] = "X-Requested-With, Access-Control-Allow-Origin, X-HTTP-Method-Override, Content-Type, Authorization, Accept"; | |
headers['Content-Type'] = 'application/json'; | |
response.writeHead(200, headers); | |
response.write(arquivo); | |
response.end(); | |
}); | |
}); | |
server.listen(8000, function() { | |
console.log('Executando Servidor HTTP'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment