-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
58 lines (50 loc) · 1.48 KB
/
Copy pathserver.js
File metadata and controls
58 lines (50 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const express = require("express");
const bodyParser = require("body-parser");
const path = require('path');
const mongoose = require('mongoose');
const bluebird = require('bluebird');
const cors = require('cors');
const app = express();
app.use(bodyParser.json());
app.use(cors());
const farmers = [
{
id: 1,
email: 'john_appleseed@farms.com',
password: 'appleseed',
location: 'Charleston, SC',
rating: '4',
name: 'John Appleseed'
},
{
id: 2,
email: 'martha_beetboxer@agriculture.com',
password: 'beetboxer',
location: 'Walterboro, SC',
rating: '5',
name: 'Martha Beetboxer'
}
]
// // Connect to DB
mongoose.connect(process.env.MONGODB_URI, { useMongoClient: true });
mongoose.Promise = bluebird;
mongoose.connection.on('error', (err) => {
console.error(`Could not establish connection to MongoDB: ${err.message}`);
});
// Create link to Angular build directory
var distDir = __dirname + "/dist/";
app.use(express.static(distDir));
app.post('/api/login', (req, res) => {
const fahmah = farmers.filter(farmer =>
farmer.email === req.body.email && farmer.password === req.body.password
);
return fahmah.length ? res.status(200).send(fahmah[0]) : res.status(500).send();
});
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'))
})
// Initialize the app.
const server = app.listen(process.env.PORT || 8080, function () {
const port = server.address().port;
console.log("App now running on port", port);
});