-
Notifications
You must be signed in to change notification settings - Fork 65
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
Changes from 9 commits
b53fee8
9e461fa
66f996f
a00a970
92dc3ce
0362dc3
eff6655
f96576a
2082e72
5a19855
34dbe2b
aa51be5
d823742
84331d7
17fab19
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
.vscode | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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/). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
###### MongoDb | ||
- Download and Install [MongoDB Community Edition](https://docs.mongodb.com/manual/installation/#mongodb-community-edition). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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/) | ||
|
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:- |
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:- |
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({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
path: `${__dirname}/.${process.env.NODE_ENV}.env`, | ||
}); | ||
|
||
console.log('NODE_ENV ->', process.env.NODE_ENV); | ||
console.log('MONGODB_URI ->', process.env.MONGODB_URI); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a |
||
"express": "^4.15.3", | ||
"express-session": "^1.15.3", | ||
"mongodb": "^2.2.27", | ||
"mongoose": "^4.10.4" | ||
} | ||
} |
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; | ||
|
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; |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
module.exports.Repository = Repository; |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
|
||
|
||
module.exports.User = User; |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can use |
||
|
||
module.exports.Username = Username; |
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; |
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; |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're modifying |
||
_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', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also we should try not to nest There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
describe('User', () => { | ||
it('Should save user to db', (done) => { | ||
user.save().then((usr) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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)); | ||
}); | ||
}); | ||
}); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍