Skip to content

Commit 3410d2c

Browse files
committed
init commits
1 parent b2f9589 commit 3410d2c

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

server.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const express = require('express'),
2+
bodyParser = require('body-parser'),
3+
path = require('path'),
4+
http = require('http'),
5+
app = express();
6+
7+
// API file for interacting with MongoDB
8+
const api = require('./server/routes/api');
9+
10+
// Parsers
11+
app.use(bodyParser.json());
12+
app.use(bodyParser.urlencoded({ extended: false}));
13+
14+
// Angular DIST output folder
15+
app.use(express.static(path.join(__dirname, 'dist')));
16+
17+
// API location
18+
app.use('/api', api);
19+
20+
// Send all other requests to the Angular app
21+
app.get('*', (req, res) => {
22+
res.sendFile(path.join(__dirname, 'dist/index.html'));
23+
});
24+
25+
//Set Port
26+
const port = process.env.PORT || '8080';
27+
app.set('port', port);
28+
29+
const server = http.createServer(app);
30+
31+
server.listen(port, () => console.log(`Running on localhost:${port}`));

server/routes/api.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const express = require('express'),
2+
router = express.Router(),
3+
MongoClient = require('mongodb').MongoClient,
4+
ObjectID = require('mongodb').ObjectID;
5+
6+
// Connect
7+
const connection = (callback) => {
8+
return MongoClient.connect('mongodb://localhost:27017/mean-to-the-bone-db', (err, db) => {
9+
if (err) return console.log(err);
10+
callback(db);
11+
});
12+
};
13+
14+
// Error handling
15+
const sendError = (err, res) => {
16+
response.status = 501;
17+
response.message = typeof err == 'object' ? err.message : err;
18+
res.status(501).json(response);
19+
};
20+
21+
// Response handling
22+
let response = {
23+
status: 200,
24+
data: [],
25+
message: null
26+
};
27+
28+
// Get users
29+
router.get('/users', (req, res) => {
30+
connection((db) => {
31+
db.collection('users')
32+
.find()
33+
.toArray()
34+
.then((users) => {
35+
response.data = users;
36+
res.json(response);
37+
})
38+
.catch((err) => {
39+
sendError(err, res);
40+
});
41+
});
42+
});
43+
44+
module.exports = router;

src/app/cms.service.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { TestBed, inject } from '@angular/core/testing';
2+
3+
import { CmsService } from './cms.service';
4+
5+
describe('CmsService', () => {
6+
beforeEach(() => {
7+
TestBed.configureTestingModule({
8+
providers: [CmsService]
9+
});
10+
});
11+
12+
it('should be created', inject([CmsService], (service: CmsService) => {
13+
expect(service).toBeTruthy();
14+
}));
15+
});

src/app/cms.service.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Injectable } from '@angular/core';
2+
3+
import { Http, Headers, RequestOptions } from '@angular/http';
4+
import 'rxjs/add/operator/map';
5+
6+
@Injectable()
7+
export class CmsService {
8+
9+
result: any;
10+
11+
constructor(private _http: Http) { }
12+
13+
getUsers() {
14+
return this._http.get("/api/users")
15+
.map(result => this.result = result.json().data);
16+
}
17+
18+
}

0 commit comments

Comments
 (0)