Skip to content
Snippets Groups Projects
Commit a62ff657 authored by TheophilePACE's avatar TheophilePACE
Browse files

minimal Boilerplate added

parent dc52eb20
No related branches found
No related tags found
No related merge requests found
/dist
/logs
/npm-debug.log
/node_modules
.DS_Store
FROM alpine:3.4
# File Author / Maintainer
LABEL authors="Zouhir Chahoud <zouhir@zouhir.org>"
# Update & install required packages
RUN apk add --update nodejs bash git
# Install app dependencies
COPY package.json /www/package.json
RUN cd /www; npm install
# Copy app source
COPY . /www
# Set work directory to /www
WORKDIR /www
# set your port
ENV PORT 8080
# expose the port to outside world
EXPOSE 8080
# start command as per package.json
CMD ["npm", "start"]
LICENSE 0 → 100755
The MIT License (MIT)
Copyright (c) 2016 Jason Miller
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
web: npm start
version: '3'
services:
mongo:
image: mongo:3.6
web:
build: .
ports:
- "8080:8080"
environment:
- MONGODB_URI=mongodb://mongo:27017/test
links:
- mongo
depends_on:
- mongo
volumes:
- .:/starter
- /starter/node_modules
\ No newline at end of file
This diff is collapsed.
{
"name": "logistapi",
"version": "0.3.0",
"description": "Express API of logisteam project",
"main": "dist",
"scripts": {
"dev": "nodemon -w src --exec \"babel-node src --presets es2015,stage-0\"",
"build": "babel src -s -D -d dist --presets es2015,stage-0",
"start": "node dist",
"prestart": "npm run -s build",
"test": "eslint src"
},
"eslintConfig": {
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 7,
"sourceType": "module"
},
"env": {
"node": true
},
"rules": {
"no-console": 0,
"no-unused-vars": 1
}
},
"repository": "logisteam",
"author": "Théophile Pace <theophilepace@gmail.com>",
"license": "MIT",
"dependencies": {
"body-parser": "^1.13.3",
"compression": "^1.5.2",
"cors": "^2.7.1",
"express": "^4.13.3",
"morgan": "^1.8.0",
"resource-router-middleware": "^0.6.0"
},
"devDependencies": {
"babel-cli": "^6.9.0",
"babel-core": "^6.9.0",
"babel-preset-es2015": "^6.9.0",
"babel-preset-stage-0": "^6.5.0",
"eslint": "^3.1.1",
"nodemon": "^1.9.2"
}
}
import resource from 'resource-router-middleware';
import facets from '../models/facets';
export default ({ config, db }) => resource({
/** Property name to store preloaded entity on `request`. */
id : 'facet',
/** For requests with an `id`, you can auto-load the entity.
* Errors terminate the request, success sets `req[id] = data`.
*/
load(req, id, callback) {
let facet = facets.find( facet => facet.id===id ),
err = facet ? null : 'Not found';
callback(err, facet);
},
/** GET / - List all entities */
index({ params }, res) {
res.json(facets);
},
/** POST / - Create a new entity */
create({ body }, res) {
body.id = facets.length.toString(36);
facets.push(body);
res.json(body);
},
/** GET /:id - Return a given entity */
read({ facet }, res) {
res.json(facet);
},
/** PUT /:id - Update a given entity */
update({ facet, body }, res) {
for (let key in body) {
if (key!=='id') {
facet[key] = body[key];
}
}
res.sendStatus(204);
},
/** DELETE /:id - Delete a given entity */
delete({ facet }, res) {
facets.splice(facets.indexOf(facet), 1);
res.sendStatus(204);
}
});
import { version } from '../../package.json';
import { Router } from 'express';
import facets from './facets';
export default ({ config, db }) => {
let api = Router();
// mount the facets resource
api.use('/facets', facets({ config, db }));
// perhaps expose some API metadata at the root
api.get('/', (req, res) => {
res.json({ version });
});
return api;
}
{
"port": 8080,
"bodyLimit": "100kb",
"corsHeaders": ["Link"]
}
export default callback => {
// connect to a database if needed, then pass it to `callback`:
callback();
}
import http from 'http';
import express from 'express';
import cors from 'cors';
import morgan from 'morgan';
import bodyParser from 'body-parser';
import initializeDb from './db';
import middleware from './middleware';
import api from './api';
import config from './config.json';
let app = express();
app.server = http.createServer(app);
// logger
app.use(morgan('dev'));
// 3rd party middleware
app.use(cors({
exposedHeaders: config.corsHeaders
}));
app.use(bodyParser.json({
limit : config.bodyLimit
}));
// connect to db
initializeDb( db => {
// internal middleware
app.use(middleware({ config, db }));
// api router
app.use('/api', api({ config, db }));
app.server.listen(process.env.PORT || config.port, () => {
console.log(`Started on port ${app.server.address().port}`);
});
});
export default app;
/** Creates a callback that proxies node callback style arguments to an Express Response object.
* @param {express.Response} res Express HTTP Response
* @param {number} [status=200] Status code to send on success
*
* @example
* list(req, res) {
* collection.find({}, toRes(res));
* }
*/
export function toRes(res, status=200) {
return (err, thing) => {
if (err) return res.status(500).send(err);
if (thing && typeof thing.toObject==='function') {
thing = thing.toObject();
}
res.status(status).json(thing);
};
}
import { Router } from 'express';
export default ({ config, db }) => {
let routes = Router();
// add middleware here
return routes;
}
// our example model is just an Array
const facets = [];
export default facets;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment