Victor Leung
Victor Leung
BlogFlower shop

Setup proxy server with Express

February 20, 2015

The problem:

I am working on a project using BreweryDB. I was trying to load some data from the API, but they don’t support jsonp. There is a CORS issue if I directly fetch data using Angular:

    XMLHttpRequest cannot load []( No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

My solution:

I don’t want to expose my API key, so I have to setup an intermediate proxy. The following code illustrates step by step on how to setup a proxy using node/express.

Step 1: Install express and request

    npm install express --save npm install request --save

Step 2: Create server.js

    var express = require('express');
    var request = require('request');
    var app = express();

Step 3: Setup the route (replace API_KEY with your API key)

    app.get('/api', function(req, res){
      request('' + API_KEY, function (error, response, body) {
        if (!error && response.statusCode === 200) {

Step 4: Setup the port

    console.log('Server running on port %d', 3000);

Step 5: Start the server (node server.js)

Open your brower at http://localhost:3000/api, you should be able the get the json object and log in your browser console:

    "message":"Request Successful",
    "data":"You have reached the API. For access, check out",

Send me an email if you have any problem ☺

About Victor Leung

Software development professional with expertise in application architecture, cloud solutions deployment, and financial products development. Possess a Master's degree in Computer Science and an MBA in Finance. Highly skilled in AWS (Certified Solutions Architect Professional, Developer and SysOps Administrator), GCP (Professional Cloud Architect), Microsoft Azure, Kubernetes(CKA, CKAD, CKS, KCNA), and Scrum(PSM, PSPO) methodologies.

Happy to connect

Continuous improvement

Copyright © 2023.