Create a Simple Web Service

Use this guide to learn how to create a Simple Web Service to send API Calls to the FaceTec Server.

Prerequisite

  1. Install Node.js.
  2. Run node -v (Using the latest version is recommended).

Create a Basic Web Service

  1. Use npm to create a new instance for the Web Service: npm init -y.
  2. Use npm to install express: npm install express.
  3. Create a file named server.js and add it as the start script underneath "scripts" in package.json.
    "start": "node server.js",
  4. Update server.js with the code to run as a Web Service.
    var http = require('http');
    const express = require('express');
    const app = express();
    app.get('/status', (req, res) => {
        res.send('Request Received');
    });
    
    const PORT = 8080;
    app.listen(PORT, () => {
        console.log(`Listening on Port ${PORT}...`);
    });
  5. Run the Web Service: npm start.

Call and Return the FaceTec Server's /status HTTPS API.

To call and return the FaceTec Server's HTTPS API status, update the server.js code as shown below. This JavaScript file can also be downloaded here.

var http = require('http');
const express = require('express');
const app = express();
// Create the /status API for the Middle-Layer Web Service.
app.get('/status', (req, res) => {
    // Make the Middle-Layer Web Service's /status API call the FaceTec Server's /status API.
    // <SERVER_SDK_URL> should be replaced with the URL or IP Address of where Your FaceTec Server is hosted.
    http.get('http://<SERVER_SDK_URL>:8080/status', (response) => {
        const { statusCode } = response;
        const contentType = response.headers['content-type'];
        let error;
        // Return an error if the API Request to the FaceTec Server fails.
        if (statusCode != 200) {
            error = new Error(`Request Failed. Status Code: ${statusCode}`);
        }
        if (error) {
            console.error(error.message);
            res.send(error.message);
            response.resume();
            return;
        }

        // Receive the API Response from the FaceTec Server.
        response.setEncoding('utf8');
        let statusData = '';
        response.on('data', (chunk) => { statusData += chunk; });
        response.on('end', () => {
            try {
                const jsonData = JSON.parse(statusData);
                console.log(jsonData);
                res.send(jsonData);
            }
            catch (e) {
                console.error(e.message);
                res.send(e.message);
            }
        });
    }).on('error', (e) => {
          console.error(`Error: ${e.message}`);
          res.send(e.message);
    });
});

const PORT = 8080;
app.listen(PORT, () => {
    console.log(`Listening on Port ${PORT}...`);
});