1
0
Fork 0

Add nodejs API to server the brands.json

master
Blaine Motsinger 2 years ago
parent b27ece8f22
commit 053669cfae
  1. 6
      .us-pg-brands-rc
  2. 8
      lib/config.js
  3. 8
      lib/data.js
  4. 39
      lib/response.js
  5. 7
      package.json
  6. 15
      routes/root.js
  7. 51
      server.js

@ -0,0 +1,6 @@
{
"app": {
"port": 5000,
"address": "0.0.0.0"
}
}

@ -0,0 +1,8 @@
// us-pg-brands application settings
const fs = require( 'fs' );
const path = require( 'path' );
const config = JSON.parse( fs.readFileSync( path.resolve( __dirname, './../.us-pg-brands-rc' ), 'utf8' ) );
module.exports = config;

@ -0,0 +1,8 @@
// data loader
const fs = require( 'fs' );
const path = require( 'path' );
const data = JSON.parse( fs.readFileSync( path.resolve( __dirname, './../data/brands.json' ), 'utf8' ) );
module.exports = data;

@ -0,0 +1,39 @@
// status codes and strings for the routes
const status = {
// successes
'HTTP_OK' : {
code : '200',
string : 'OK.',
},
// client errors
'HTTP_BAD_REQUEST' : {
code : '400',
string : "Whoops, something isn't correct with your request.",
},
'HTTP_UNAUTHORIZED' : {
code : '401',
string : 'You are not authenticated.',
},
'HTTP_FORBIDDEN' : {
code : '403',
string : 'You are not authorized to access this resource.',
},
'HTTP_NOT_FOUND' : {
code : '404',
string : 'That resource was not found.',
},
'HTTP_CONFLICT' : {
code : '409',
string : 'That resource already exists.',
},
// server errors
'HTTP_INTERNAL_SERVER_ERROR' : {
code : '500',
string : 'Whoops, something went wrong on our end.',
},
};
module.exports.status = status

@ -0,0 +1,7 @@
{
"dependencies": {
"express": "^4.17.1",
"morgan": "^1.10.0",
"path": "^0.12.7"
}
}

@ -0,0 +1,15 @@
// root routes
const express = require( 'express' );
const router = express.Router();
const data = require( './../lib/data' );
const response = require( './../lib/response' );
router.get( '/', function ( req, res ) {
res.status( response.status.HTTP_OK.code )
.header( 'Content-Type', 'application/json' )
.header( 'Connection', 'close' )
.send( data );
});
module.exports = router;

@ -0,0 +1,51 @@
// us-pg-brands
'use strict';
const version = '0.0.1';
const express = require( 'express' );
const config = require( './lib/config' );
const app = express();
const logger = require( 'morgan' );
const response = require( './lib/response' );
// disable unused headers
app.disable( 'etag' );
app.disable( 'x-powered-by' );
// routes
const root = require( './routes/root' );
app.use( logger( 'combined' ) );
// load the routes
app.use( '/', root );
// default routes
// NOT FOUND for any GET requests
app.get( '*', function( req, res ) {
res.status( response.status.HTTP_NOT_FOUND.code )
.header( 'Content-Type', 'text/plain' )
.header( 'Connection', 'close' )
.send( response.status.HTTP_NOT_FOUND.string );
});
// BAD REQUEST for anything else
app.use( function( req, res ) {
res.status( response.status.HTTP_BAD_REQUEST.code )
.header( 'Content-Type', 'text/plain' )
.header( 'Connection', 'close' )
.send( response.status.HTTP_BAD_REQUEST.string );
});
console.log( '[info] us-pg-brands - version ' + version );
app.listen( config.app.port, config.app.address, function() {
console.log(
'[info] server started\n' +
'[info] serving: ' + config.app.address + ':' + config.app.port
);
});
module.exports = app;
Loading…
Cancel
Save