-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
64 lines (48 loc) · 1.82 KB
/
main.js
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
58
59
60
61
62
63
64
const express = require('express');
const bodyParser = require('body-parser');
// create express app
const app = express();
const router = express.Router();
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }))
// parse application/json
app.use(bodyParser.json())
app.use("/",router);
app.use('/assets', express.static('app/views/assets'));
// Configuring the database connection
const dbConfig = require('./config/database.config.js');
const mongoose = require('mongoose');
mongoose.connect(dbConfig.url);
mongoose.connection.on('error', function() {
console.log('Could not connect to the database. Exiting now...');
process.exit();
});
mongoose.connection.once('open', function() {
console.log("Successfully connected to the database");
})
const student = require('./app/controllers/students.controller.js');
// define a simple route
router.get('/api', function(req, res){
res.json({"message": "Welcome to Students application REST-ful API. Organize and keep track of all your students!"});
});
// Create a new student
router.post('/api/student', student.create);
// Retrieve all students
router.get('/api/students', student.findAll);
// Retrieve a single student with studentId
router.get('/api/student/:studentId', student.findOne);
router.get('/api/subjects/:subject', student.findBySubject);
router.get('/api/genders/:gender', student.findByGender);
router.get('/api/age/:age', student.findByAge);
// Update a student with studentId
router.put('/api/student/:studentId', student.update);
// Delete a student with studentId
router.delete('/api/student/:studentId', student.delete);
// Web
router.get("/",function(req,res){
res.sendFile('index.html', { root: 'app/views' })
});
// listen for requests
app.listen(3000, function(){
console.log("Server is listening on port 3000");
});