
I am stuck again. Starting another project that I’m probably not going to finish but I know I will get some learning out of it.
Primarily, I wont on my client’s client side of their application. I’m not required to touch a lot of server code at all. I do think all front-end developers should at least understand how to build a simple backend.
About my app
I am building a crime map with advanced details on crime statistics from any zip code. Being able retrieve data on police agencies is my first step to getting the data I need. Right now I am laying out the infrastructure for a scalable and highly interactive application that can be used by the general public and useful to large and small law enforcement agencies.
I will be using Axios
and Express
for this project’s backend.
Building the Server
When making API calls on the client side, I usually get slammed with a cross-origin-request errors (CORS). There are work arounds to it but I’d rather simplify things by separating the logic.
I am using the env variable to not expose my API key. Our cors()
middleware handles or cross-origin-request so that we can get a response successfully from the source.
Creating the endpoint /agencies/agency
allows me to retrieve a single police agency based on the ORI number provided by the documentation more safely in an environment I have more control over.
require('dotenv').config()
const express = require('express')
const path = require("path");
const app = express()
const axios = require('axios')
const cors = require('cors')
const agencyAPI = require('./lib/agencyApi')
app.use(cors())
app.get('/agencies/agency', async (req, res) => {
//req.body.param needed to get the form data from the front to feed to API
const data = await agencyAPI.getAgency(process.env.DETRTOIT_POLICE_ORI)
if(data) res.send(data.data.agency_name)
})
app.listen(${process.env.PORT}`))