Real time database NodeJs and Firebase restfull Api
database
Firebase
NodeJs
API
- By Code solution
- Jan 20th, 2021
- 0 comments
- 0
Create server-side rest API using NodeJs and real-time database(Firebase).Npm Firebase package to create server-side rest API.
Project Structure :
Project Name // Project directory - node_modules // Npm Package - package-lock.json. // package-lock.json - package.json // package.json - server.js // server.js file - index.html // Html file - testapp.js // firebase json file
Create Project on Firebase console
Link: https://console.firebase.google.com/
then go to the project setting -> SERVICE ACCOUNTS
click then button Generate new private key
download the JSON file of your project
Then go to left side menu click Database and change the rules like that
{ "rules": { ".read": true, ".write": true } }
Then Create a new Directory, we can run the following command:
mkdir <directoryname>
Then move into the newly created directory:
cd <directoryname>
then run this command :
npm init
and fill this information like that :
package name: (directoryname) version: (1.0.0) description: entry point: (index.js) server.js test command: git repository: keywords: author: license: (ISC) {"name": "curlapi", "version": "1.0.0","description": "", "main": "server.js","scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC"}Is this ok? (yes) yes then enter
After creating project install node package run command:
npm install --save express morgan body-parser method-override multer, fs, firebase
create a server.js file and add this code :
// set up ======================== var express = require('express'); var app = express(); // create our app w/ express var Firebase = require('firebase'); var morgan = require('morgan'); var bodyParser = require('body-parser'); // pull information from HTML POST (express4) var methodOverride = require('method-override'); var multer = require('multer'); var fs = require("fs"); app.use(function(req, res, next) { //allow cross origin requests res.setHeader("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Methods", "POST, PUT, OPTIONS, DELETE, GET"); res.header("Access-Control-Max-Age", "3600"); res.header("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"); next(); }); Firebase.initializeApp({ databaseURL: "https://testapp-e5455.firebaseio.com/", serviceAccount: './testapp.json', //this is file that I downloaded from Firebase Console }); var db = Firebase.database(); var usersRef = db.ref("users"); // var FirebaseRef = new Firebase("https://testapp-e5455.firebaseio.com/"); // configuration app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users app.use('/public/uploads',express.static(__dirname + '/public/uploads')); app.use(morgan('dev')); // log every request to the console app.use(bodyParser.urlencoded({'extended':'true'})); // parse application/x-www-form-urlencoded app.use(bodyParser.json()); // parse application/json app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json app.get('/', function (req, res) { res.sendfile('./index.html') }) // create user app.post('/api/createUser', function(req, res) { // var userEmail = req.body.user_email; var data = req.body; usersRef.push(data, function(err) { if (err) { res.send(err) } else { // var key = Object.keys(snapshot.val())[0]; // console.log(key); res.json({message: "Success: User Save.", result: true}); } }); }); // update user app.put('/api/updateUser', function(req, res) { var uid = "-Ks8HilZxX5vtFPqGu75"; var data = req.body; usersRef.child(uid).update(data, function(err) { if (err) { res.send(err); } else { usersRef.child("uid").once("value", function(snapshot) { if (snapshot.val() == null) { res.json({message: "Error: No user found", "result": false}); } else { res.json({"message":"successfully update data", "result": true, "data": snapshot.val()}); } }); } }); }); // delete user app.delete('/api/removeUser', function(req, res) { var uid = "-Ks8HilZxX5vtFPqGu75"; usersRef.child(uid).remove(function(err) { if (err) { res.send(err); } else { res.json({message: "Success: User deleted.", result: true}); } }) }); // get users app.post('/api/getUsers', function(req, res) { var uid = "-Ks8HilZxX5vtFPqGu75"; if (uid.length != 20) { res.json({message: "Error: uid must be 20 characters long."}); } else { usersRef.once("value", function(snapshot) { //console.log(snapshot); if (snapshot.val() == null) { res.json({message: "Error: No user found", "result": false}); } else { res.json({"message":"successfully fetch data", "result": true, "data": snapshot.val()}); } }); } }); // login app.post('/api/login', function(req, res) { User.findOne({ 'user_name' : req.body.user_name, 'password' : req.body.password }, function(err, user) { // if there are any errors, return the error if (err) return res.send(err); // check to see if user exist if (user) { return res.json(user); } else { return res.json({"message":"Invalid Username or Password."}); } }); }); app.listen(3000); console.log("port is 3000");
create Index.html file and add this code :
<!doctype html> <html> <head> <title>Firebase</title> <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.css"> </head> <body> <h2>Firebase Hello</h2> </body> </html>