Code Monkey home page Code Monkey logo

facial-recognition-server's Introduction

facial-recognition-server

Live: https://jayli3n.github.io/facial-recognition-web-app/

Node.js server to handle our facial recognition web app.

API Used: Clarifai
Server is deployed on Heroku.

Goals

  • Familiarize myself with server side development with Node.js using Express.js
  • Understand the importance of storing user data securely and privately by hashing their credentials using bcrypt
  • Know how and when to use middlewares with Express.js such as Knex.js, Body-Parser and cors
  • Be able to use relational databases to store key information about the user, database used here was PostgreSQL

Sample Code:


Below is a code snippet to illustrate the usage of different technologies.

Initialise the server with database & middlewares:
const db = knex({
  client: 'pg',
  connection: {
    connectionString: process.env.DATABASE_URL,
    ssl: true
  }
});

const app = express();
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
	console.log('Server is running on port: ', PORT);
});
app.use(bodyParser.json());
app.use(cors2());
Request Handlers:
app.post('/signin', (req, res) => {signinC.handleSignIn(req, res, db, bcrypt)});
app.post('/register', (req, res) => {registerC.handleRegister(req, res, db, bcrypt)});
app.get('/profile/:id', (req, res) => {profileC.handleProfile(req, res, db)});
app.put('/image', (req, res) => {imageC.handleImage(req, res, db)});
app.post('/imageUrl', (req, res) => {imageUrl.handleApiCall(req, res)});
app.get('/', (req, res) => {res.json('Live: https://github.com/jayli3n/facial-recognition-web-app')});
Sample Handler (register):

This is a good one to showcase because it uses bcrypt to first hash the password, then writes the new user's credentials to the database by using a transaction token so that multiple writes to different tables in the db will occur successfully, else the whole operation is cancelled.

const handleRegister = (req, res, db, bcrypt) => {
	const {email, name, password} = req.body;
	const hash = bcrypt.hashSync(password);
	db.transaction(trx => {
		trx.insert({
			email: email,
			hash: hash
		})
		.into('login')
		.returning('email')
		.then(loginEmail => {
			trx('users')
			.insert({
				name: name,
				email: loginEmail[0],
				joined: new Date()
			}).returning('*')
			.then(user => {
				res.json(user[0]);
			})
			.then(trx.commit)
			.catch(trx.rollBack)
		}).catch(err => res.status(400).json('Unable to register.'))
	}).catch(err => res.status(400).json('Unable to register.'));
}

module.exports = {
	handleRegister: handleRegister
};

Database

Install PostgreSQL with brew.

  • to create: createdb <database_name>
  • to enter: psql <database_name>
  • to drop: dropdb <database_name>
  • to list: psql -l

NPM Packages Used:

"dependencies": {
    "bcrypt-nodejs": "0.0.3",
    "body-parser": "^1.18.3",
    "clarifai": "^2.9.0",
    "cors": "^2.8.5",
    "express": "^4.16.4",
    "knex": "^0.16.5",
    "pg": "^7.10.0"
 }

NPM Dev Packages:

"devDependencies": {
    "nodemon": "^1.18.11"
 }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.