Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Building a backend #20 #40

Merged
merged 15 commits into from
Jun 3, 2017
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.vscode
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,62 @@ I've collected usernames of amazing stargazers :star: in Github. By using Github

You need to submit Github Token to access Github API.

## Getting Started
##### Pre-Installation Requirements
###### Node
- Download and install latest stable version of [Node](https://nodejs.org/en/download/).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should specify what version are we using and add that to the package.json under "engines" too.


###### MongoDb
- Download and Install [MongoDB Community Edition](https://docs.mongodb.com/manual/installation/#mongodb-community-edition).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same goes for Mongo, what version should we (everybody contributing) should use.


- Create new data directory for mondoDB.
```bash
mkdir -p </path/to/mongodb-data>
```
- Run MongoDB
```bash
mongod --dbpath </path/to/mongodb-data>
```


Now you should have mongoDB server running. If any issue please visit [mongoDB Doc](https://docs.mongodb.com/manual/installation/#tutorials).

Open another terminal/Command Prompt to clone this App.

##### Installing and running curiosity

```bash
# Rest of the guide assumes you already have MongoDB installed and MongoDB server is running.

# Get the latest version
git clone https://github.com/curiositylab/curiosity

# Change directory
cd curiosity

# First time install only
yarn install

# Start the app
yarn run start
```
If installation is successful You should see following message. visit 'http://localhost:3000' to view website.
```
NODE_ENV -> dev
MONGODB_URI -> mongodb://127.0.0.1:27017/curiosity
Starting server on port 3000.
```

Other Scripts for developement and testing.
```bash
# run test for the app
yarn run test-node

# Start app with watch (10s delay)
yarn run watch
```


## Tools I used

* [SweetAlert2](https://limonte.github.io/sweetalert2/)
Expand Down
12 changes: 12 additions & 0 deletions config/.dev.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Node ENV for DEVELOPMENT
# Update PRIVATE section below with correct value
#
#
#


# Public section:-
MONGODB_URI='mongodb://127.0.0.1:27017/curiosity'


# PRIVATE section:-
13 changes: 13 additions & 0 deletions config/.test.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Node ENV for TESTING
# Update PRIVATE section below with correct value
#
#
#


# Public section:-
MONGODB_URI='mongodb://127.0.0.1:27017/curiosityTEST'



# PRIVATE section:-
10 changes: 10 additions & 0 deletions config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = 'dev';
}

let dotenv = require('dotenv').config({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const instead of let

path: `${__dirname}/.${process.env.NODE_ENV}.env`,
});

console.log('NODE_ENV ->', process.env.NODE_ENV);
console.log('MONGODB_URI ->', process.env.MONGODB_URI);
14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@
"mocha": "^3.4.2"
},
"scripts": {
"lint": "eslint js/**/*.js"
"lint": "eslint js/**/*.js",
"test-node": "NODE_ENV=test mocha test/**.test.js",
"watch": "nodemon --delay 10 server/app.js",
"start": "node server/app.js"
},
"dependencies": {}
"dependencies": {
"dotenv": "^4.0.0",
"expect": "^1.20.2",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a devDependency instead of a dependency

"express": "^4.15.3",
"express-session": "^1.15.3",
"mongodb": "^2.2.27",
"mongoose": "^4.10.4"
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions server/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require('./../config/config');

const express = require('express');
const { mongoose } = require('./db/mongoose');
const { usernameRoutes } = require('./routes/api/username');
const { userRoutes } = require('./routes/user');

const app = express();
const port = process.env.PORT || 3000;

app.use(express.static(`${__dirname}/../public`));

app.use('/user',userRoutes);
app.use('/api/username', usernameRoutes);

app.listen(port, () => {
console.log(`Starting server on port ${port}.`);
});

module.exports.app = app;

9 changes: 9 additions & 0 deletions server/db/mongoose.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let mongoose = require('mongoose');

mongoose.Promise = global.Promise;

mongoose.connect(process.env.MONGODB_URI, (err) => {
if (err) throw err;
});

module.exports = mongoose;
21 changes: 21 additions & 0 deletions server/db/repositories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Repositories
const mongoose = require('./mongoose');

const Schema = mongoose.Schema;

const repositorySchema = new Schema({
_id: mongoose.Schema.Types.ObjectId,
name: String,
html_url: String,
description: String,
stargazers_count: Number,
forks_count: Number,
created_at: Date,
updated_at: Date,
language: String,
});


let Repository = mongoose.model('Repository', repositorySchema);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const instead of let


module.exports.Repository = Repository;
19 changes: 19 additions & 0 deletions server/db/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Schema for logged in users.
const mongoose = require('./mongoose');

const Schema = mongoose.Schema;

const userSchema = new Schema({
_id: mongoose.Schema.Types.ObjectId,
githubId: Number,
login: String,
name: String,
html_url: String,
accessToken: String,
});


let User = mongoose.model('User', userSchema);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use const instead of let here too.



module.exports.User = User;
24 changes: 24 additions & 0 deletions server/db/username.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Schema of usernames of stargazers in Github
const mongoose = require('./mongoose');

const Schema = mongoose.Schema;

const usernameSchema = new Schema({
_id: mongoose.Schema.Types.ObjectId,
githubId: Number,
login: String,
name: String,
html_url: String,
location: String,
bio: String,
public_repos: Number,
public_gists: Number,
followers: Number,
dbLastUpdated: Date,
starredIds: [mongoose.Schema.Types.ObjectId],
});


let Username = mongoose.model('Username', usernameSchema);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can use const in here.


module.exports.Username = Username;
51 changes: 51 additions & 0 deletions server/routes/api/username.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// API related to username here.

const express = require('express');

const router = express.Router();

const USERNAMES = [
'tj',
'addyosmani',
'paulirish',
'passy',
'sindresorhus',
'gaearon',
'defunkt',
'daimajia',
'kvz',
'omgmog',
'yyx990803',
'kennethreitz',
'Trinea',
'JacksonTian',
'substack',
'stormzhang',
'muan',
'onevcat',
'clowwindy',
'getify',
'csu',
'matiasinsaurralde',
'ibireme',
'phodal',
'ryanb',
'isaacs',
'justjavac',
'ChenYilong',
'cusspvz',
'feross',
'm1guelpf',
'brentvatne',
'developit',
'Kureev',
'mxstbr',
'jaredly',
];


router.get('/all', (req, res) => {
res.send(USERNAMES);
});

module.exports.usernameRoutes = router;
20 changes: 20 additions & 0 deletions server/routes/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// logged in/Registered user end point here
const express = require('express');

const router = express.Router();

// POST '/login'
// delete '/logout'
router.get('/', (req, res) => {
res.send('You reached GET /user end point');
});

router.get('/login', (req, res) => {
res.send('You are logged in !..lol');
});

router.get('/logout', (req, res) => {
res.send('You are logged out !..lol');
});

module.exports.userRoutes = router;
55 changes: 55 additions & 0 deletions test/db.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require('./../config/config');

const mongoose = require('./../server/db/mongoose');
const expect = require('expect');

const { Repository } = require('./../server/db/repositories');
const { Username } = require('./../server/db/username');
const { User } = require('./../server/db/user');

// console.log(mongoose.connection.host);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code should go away. No commented code here unless there's a super strong reason 👍

// console.log(mongoose.connection.port);

let user = new User({
_id: new mongoose.Types.ObjectId(),
githubId: 1111111,
login: 'testUser1',
name: 'testUser1',
html_url: 'https://github.com/testUser1',
accessToken: 'sadjkskajfeifhwgsjfksdfkmcoiwerkhdskfjksladjf',
});

let repo = new Repository({
Copy link
Contributor

@alejandronanez alejandronanez Jun 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're modifying repo anywhere else, you can change this to a const

_id: new mongoose.Types.ObjectId(),
name: 'curiosity',
html_url: 'https://github.com/mubaris/curiosity',
description: 'Find Amazing Github Projects',
stargazers_count: 72,
forks_count: 12,
created_at: new Date(),
updated_at: new Date(),
language: 'Javascript',
});

describe('Simple DB test', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also we should try not to nest describe blocks, this can lead to unexpected behaviour if we ever need to use this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also Mocha doesn't like arrow functions at all, we should move to function() { ... }. More info here https://mochajs.org/#arrow-functions

describe('User', () => {
it('Should save user to db', (done) => {
user.save().then((usr) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we hitting the DB here? If yes we should change things, maybe consider using sinon for mocking.

expect(usr._id).toBe(user._id);
done();
})
.catch(e => done(e));
});
});
describe('Repository', () => {
it('Should save repository to db', (done) => {
repo.save().then((rep) => {
expect(rep._id).toBe(rep._id);
done();
})
.catch(e => done(e));
});
});
});


Loading