-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
48 lines (35 loc) · 1.29 KB
/
Copy pathapp.js
File metadata and controls
48 lines (35 loc) · 1.29 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
// __Dependencies__
var express = require('express');
var mongoose = require('mongoose');
var baucis = require('baucis');
// __Main Program__
// Connect to the Mongo instance
mongoose.connect('mongodb://localhost/aAa-BaUcIs-ExAmPlE-AaA');
// Create a Mongoose schema
var Vegetable = new mongoose.Schema({ name: String });
// Note that Mongoose middleware will be executed as usual
Vegetable.pre('save', function (next) {
console.log('A vegetable was saved to Mongo: %s.', this.get('name'));
next();
});
// Register the schema
mongoose.model('vegetable', Vegetable);
// Create dummy data
var names = [ 'tomato', 'turnip', 'lovage', 'snap pea', 'carrot', 'zucchini' ];
var vegetables = names.map(function (name) { return { name: name } });
// Clear the database of old vegetables
mongoose.model('vegetable').remove(function (error) {
if (error) throw error;
// Put the fresh vegetables in the database
mongoose.model('vegetable').create(vegetables, function (error) {
if (error) throw error;
// Create the API routes
baucis.rest('vegetable');
// Create the app and listen for API requests
var app = express();
app.use('/api/v1', baucis({ swagger: true }));
app.use(express.static('./static'))
app.listen(3333);
console.log('Server listening on port 3333.');
});
});