Skip to content

Commit

Permalink
post meetup api end point
Browse files Browse the repository at this point in the history
  • Loading branch information
murediane committed Jan 11, 2019
1 parent 0d33118 commit 1d917aa
Show file tree
Hide file tree
Showing 20 changed files with 324 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"presets": [
[
"env",
{
"targets": {
"node": "6.10"
}
}
]
],
"plugins": [
[
"transform-object-rest-spread",
{
"useBuiltIns": true
}
]
]
}
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"**/*"
13 changes: 13 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"parser": "babel-eslint",
"extends": "airbnb-base",
"env": {
"node": true,
"es6": true,
"jest": true
},
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
}
}
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
npm-debug.*
yarn.*
yarn-error.*
coverage
.nyc_output
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"trailingComma": "all",
"tabWidth": 2,
"printWidth": 85,
"semi": true,
"singleQuote": true
}
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: node_js
node_js:
- 'stable'
cache:
directories:
- 'node_modules'
script: npm test
after_success:
- npm run coveralls
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: cross-env NODE_ENV=production nodemon --exec babel-node ./src/server.js
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[![Build Status](https://travis-ci.org/Musigwa/SendIT-API.svg?branch=parcels-management)](https://travis-ci.org/Musigwa/SendIT-API)
[![Coverage Status](https://coveralls.io/repos/github/Musigwa/SendIT-API/badge.svg?branch=parcels-management)](https://coveralls.io/github/Musigwa/SendIT-API?branch=parcels-management)
[![Maintainability](https://api.codeclimate.com/v1/badges/eb485abfa6f431f0033e/maintainability)](https://codeclimate.com/github/Musigwa/SendIT-API/maintainability)

# SendIT-API

This is the nodejs express server boilerplate to help you setup your project structure to chick off the ground as fast as possible.

## How to get started

1. Simply clone/download or folk this repository and just open it with your favolite editor.
2. make sure you have the node enviroment installed on your computer, or follow https://nodejs.org/en/download/ to install it. it is recommended to install the LTS version. or you can install the latest if you want to play around with its new features.
3. check whether you have a complete node environment install using the following commands:
1. node -v to check your node environment version
2. npm -v to check your node package manager version
The node installation comes with npm(node package manager) by default, but you can choose either yarn https://yarnpkg.com/lang/en/docs/install/ if you prefer.
4. After all, open your project folder location in a terminal and type one of the following commands:

### yarn install (if you are using yarn as your package manager) or

### npm install (if you are using npm as your package manager)

### can i start the server locally and play around with it on my own (?)

Obviously, yes. after getting the environment set up, you can open the project location in your terminal and te type yarn server or npm server,. you'll see the logs telling you the port on which your server is using or throws an error if any.

## Can i contribute or build my own from this server(?)

Yes you can use this node server to build your own. Once cloned/downloaded or folked, feel free to open any of the files contained in either folders and start editing the codes to see the changes.

Or if you want to contribute to this repo, please pull the request to join me.

## Having trouble using this project(?)

1. Email: [email protected]
2. Mobile || Whatsapp: +250785782928
3. Twitter: #PacificMusigwa

### Every question is welcome
5 changes: 5 additions & 0 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"env": {
"JWT_KEY": "your_key"
}
}
48 changes: 48 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "appname",
"version": "1.0.0",
"description": "add the project description here",
"main": "./src/server.js",
"author": "MUREKATETE Diane",
"license": "MIT",
"scripts": {
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"test": "nyc --reporter=text --reporter=lcov mocha --timeout 25000 --require babel-register {tests,tests/**}/*.spec.js --exit || true",
"server": "cross-env NODE_ENV=dev nodemon --exec babel-node ./src/server/index.js"
},
"engines": {
"node": "8.12.0"
},
"repository": {
"type": "git",
"url": "provide your the project repo url"
},
"dependencies": {
"babel-cli": "^6.26.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.7.0",
"bcrypt": "^3.0.2",
"bcrypt-nodejs": "0.0.3",
"cross-env": "5.2.0",
"express": "4.16.4",
"joi": "^14.0.4",
"jsonwebtoken": "8.3.0",
"morgan": "1.9.1",
"multer": "1.4.1",
"nodemon": "^1.18.6"
},
"devDependencies": {
"request": "^2.88.0",
"babel-eslint": "^10.0.1",
"chai": "4.2.0",
"chai-http": "^4.2.0",
"coveralls": "^3.0.2",
"mocha": "^5.2.0",
"nyc": "^13.1.0",
"prettier": "^1.14.3",
"prettier-eslint": "^8.8.2",
"prettier-eslint-cli": "^4.7.1",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.14.0"
}
}
4 changes: 4 additions & 0 deletions src/server/configuration/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
PORT: process.env.PORT || 3000,
baseUrl: '/api/v1',
};
29 changes: 29 additions & 0 deletions src/server/controllers/meetups.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Joi from 'joi';
import { meetups } from '../database';

const createMeetups = (req, res) => {
const schema = {
topic: Joi.string()
.min(4)
.required(),
location: Joi.string()
.min(4)
.required(),
happeningOn: Joi.string()
.min(4)
.required(),
tags: Joi.string()
.min(4)
.required(),
};
const result = Joi.validate(req.body, schema);

if (result.error) {
res.status(404).send(result.error.details[0].message);
return;
}
const meetup = { id: meetups.length + 1, ...req.body };
meetups.push(meetup);
return res.send(meetup);
};
export { createMeetups };
34 changes: 34 additions & 0 deletions src/server/database/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// write down some database configurations here
const user = {
id: 1,
firstname: 'gloria',
lastname: 'atete',
othername: 'diane',
email: '[email protected]',
phonenumber: '0782798310',
username: 'murediane',
registered: new Date(),
isadmin: true,
};
const meetups = [
{
id: 1,
createdOn: new Date('9-1-2019'),
location: 'kigali',
image: '',
topic: 'introduction to javascript',
happeningOn: new Date('Thu Jan 9 2018 15:30 GMT'),
tags: 'javascript',
},
];

const question = {
id: 1,
createdOn: new Date(),
createdBy: 1,
meetup: 1,
title: '',
body: '',
votes: 1,
};
export { user, meetups, question };
23 changes: 23 additions & 0 deletions src/server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import express from 'express';
import morgan from 'morgan';

import constants from './configuration/constants';
import middlewares from './middleware';

const server = express();
server.use(morgan('dev'));
server.use(express.json());

// Passing all of our APIs config/endpoints to our express server to use them

middlewares(server);

// Starting our Express server and pass it the port to listen to.

server.listen(constants.PORT, () => {
console.log(`server is running to port:${constants.PORT}`);
});

// Exported the server for testing purpose //

export default server;
24 changes: 24 additions & 0 deletions src/server/middleware/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import routes from '../routes';
import constants from '../configuration/constants';

export default server => {
// Headers configuration

server.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header(
'Access-Control-Allow-Headers',
'Origin,X-Requested-With,Content-Type,Accept,Authorization',
);
res.header('Access-Control-Expose-Header', 'X-Total-Count');
if (req.method === 'OPTIONS') {
res.header('Access-Control-Allow-Methods', 'PUT,GET,POST,PATCH,DELETE');
return res.status(200).json({});
}
next();
});
server.use(constants.baseUrl, routes);
server.use((req, res) =>
res.status(404).json({ message: `invalid url ${req.url}` }),
);
};
20 changes: 20 additions & 0 deletions src/server/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import express, { Router } from 'express';
import meetupRoutes from './meetup';
const routes = Router();

// API ENTRYPOINT

const entryPoint = Router();
entryPoint.get('/', (req, res) => {
res.status(200).json({ message: 'welcome' });
});

// UPLOADS ENDPOINT

const uploads = ('/uploads', express.static('uploads'));

//ALL ENDPOINTS

routes.use(entryPoint, meetupRoutes, uploads);

export default routes;
8 changes: 8 additions & 0 deletions src/server/routes/meetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Router from 'express';
import { createMeetups } from '../controllers/meetups';
const router = Router();
const entry = '/meetups';
// create Meetups route

router.post(`${entry}`, createMeetups);
export default router;
1 change: 1 addition & 0 deletions tests/controllers/yourController.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// write your tests down here
27 changes: 27 additions & 0 deletions tests/server.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import chai from 'chai';
import chaiHTTP from 'chai-http';

import server from '../src/server';
import { baseUrl } from './testData';

const should = chai.should();
chai.use(chaiHTTP);

// Tests the entrypoint of the server

describe('/GET server', () => {
it("it should return an object with property message:'welcome'", done => {
const exp = { msg: 'welcome' };
chai
.request(server)
.get(baseUrl)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('object');
res.body.should.have
.property('message')
.eql(exp.msg, `expected message property to be '${exp.msg}'`);
done();
});
});
});
5 changes: 5 additions & 0 deletions tests/testData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// write your test data down here
const baseUrl = "/api/v1";

// export the here
export { baseUrl };

0 comments on commit 1d917aa

Please sign in to comment.