-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
178 lines (146 loc) · 5.36 KB
/
server.js
File metadata and controls
178 lines (146 loc) · 5.36 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
console.log("Hello,This my first app.............");
const express = require('express');
const bodyParser= require('body-parser');
const app = express();
const MongoClient = require('mongodb').MongoClient
const mongoose = require('mongoose');
config = require('./config/config');
//Express conf !
require('./config/express.config')(app);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//Including Movies
var movies_router = express.Router();
require('./routings/movies.js')(movies_router);
app.use('/movies', movies_router);
//Including Sells
var seller_router = express.Router();
require('./routings/sellers.js')(seller_router);
app.use('/sellers', seller_router);
//Including Users
var users_router = express.Router();
require('./routings/users.js')(users_router);
app.use('/users', users_router);
//Including hall
var halls_router = express.Router();
require('./routings/halls.js')(halls_router);
app.use('/halls', halls_router);
//Including Reviews
var reviews_router = express.Router();
require('./routings/reviews.js')(reviews_router);
app.use('/reviews', reviews_router);
//Including Cities
var cities_router = express.Router();
require('./routings/cities.js')(cities_router);
app.use('/cities', cities_router);
//Including Email
var email_router = express.Router();
require('./routings/email.js')(email_router);
app.use('/email', email_router);
//Mongoose Conf !
require('./config/mongoose.config')(config);
var db
/*MongoClient.connect('mongodb://rajkumar:ticksell1234@ds115583.mlab.com:15583/ticksell', (err, database) => {
if (err) return console.log(err)
db = database
app.listen(3000, () => {
console.log('listening on 3000')
});
});*/
// BASE SETUP
// =============================================================================
var Bear = require('./models/ticksell_model');
//mongoose.connect('mongodb://rajkumar:ticksell1234@ds115583.mlab.com:15583/ticksell'); // connect to our database
mongoose.createConnection('mongodb://temp:temp@ds115583.mlab.com:15583/ticksell');
//mongoose.connect('mongodb://localhost/test'); // connect to our database
var port = process.env.PORT || 3000;
// ROUTES FOR OUR API
// =============================================================================
var router = express.Router(); // get an instance of the express Router
// middleware to use for all requests
router.use(function(req, res, next) {
// do logging
console.log('Something is happening.');
next(); // make sure we go to the next routes and don't stop here
});
// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', function(req, res) {
res.json({ message: 'hooray! welcome to our api!' });
});
// more routes for our API will happen here
router.route('/bears')
// create a bear (accessed at POST http://localhost:8080/api/bears)
.post(function(req, res) {
var bear = new Bear(); // create a new instance of the Bear model
bear.name = req.body.name; // set the bears name (comes from the request)
console.log("request:"+JSON.stringify(req.body));
// save the bear and check for errors
bear.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Bear created!' ,req: req.body});
});
})
// get all the bears (accessed at GET http://localh11ost:8080/api/bears)
.get(function(req, res) {
Bear.find(function(err, bears) {
if (err)
res.send(err);
res.json(bears);
});
});
router.route('/bears/:bear_id')
// get the bear with that id (accessed at GET http://localhost:8080/api/bears/:bear_id)
.get(function(req, res) {
Bear.findById(req.params.bear_id, function(err, bear) {
if (err)
res.send(err);
res.json(bear);
});
})
.put(function(req, res) {
// use our bear model to find the bear we want
Bear.findById(req.params.bear_id, function(err, bear) {
if (err)
res.send(err);
bear.name = req.body.name; // update the bears info
// save the bear
bear.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Bear updated!' });
});
});
})
.delete(function(req, res) {
Bear.remove({
_id: req.params.bear_id
}, function(err, bear) {
if (err)
res.send(err);
res.json({ message: 'Successfully deleted' });
});
});
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);
// START THE SERVER
// =============================================================================
//app.listen(port);
app.listen(process.env.PORT || config.dev.port, () => {
console.log('Magic happens on port ' + config.dev.port);
});
/*app.get('/', function(req, res) {
db.collection('quotes').find().toArray(function(err, results) {
console.log(results)
// send HTML file populated with quotes here
});
});
app.use(bodyParser.urlencoded({extended: true}));
app.post('/quotes', (req, res) => {
db.collection('quotes').save(req.body, (err, result) => {
if (err) return console.log(err)
console.log('saved to database');
res.redirect('/');
})
});*/