Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/************************************************
FILENAME
server_simple.js
DESCRIPTION
creates a simple web server that
display "Hello World"
HOW TO START SERVER:
1) npm install firebase-admin --save
2) node simple_server.js
3) open web browser visit http://127.0.0.1:8080
*************************************************/
// Include the HTTP Node library
// http://nodejs.org/docs/latest/api/http.html
var http = require('http');
// define the IP and port number
var localIP = "127.0.0.1"; // 127.0.0.1 is used when running the server locally
var port = 8080; // port to run webserver on
console.log("Testing hello-world server");
var admin = require("firebase-admin");
var serviceAccount = require("./GVDemo-a034f68fba6f.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://gvdemo-6f015.firebaseio.com"
});
console.log("loaded firebase admin", admin);
function sayHello(req, res) {
console.log("We've got a request for " + req.url);
// put some data in FB at the path /sams-test/1/test
var db = admin.database();
var ref = db.ref("/sams-test/1");
ref.set({test: "test"});
// HTTP response header - the content will be HTML MIME type
res.writeHead(200, {'Content-Type': 'text/html'});
// Write out the HTTP response body
res.write('<html><body>' +
'<h1>Hello world<h1>'+
'</body></html>');
// End of HTTP response
res.end();
}
/************************/
/* START THE SERVER */
/************************/
// Create the HTTP server
var server = http.createServer(sayHello);
// Turn server on - now listening for requests on localIP and port
server.listen(port, localIP);
// print message to terminal that server is running
console.log('Server running at http://'+ localIP +':'+ port +'/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment