Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require('./restful');
- require('./database');
- module.exports = function(){
- database :{
- username: "mark",
- password: "pass",
- host: "127.0.0.1",
- port: "27017",
- name: "mydb"
- };
- var config = require('./config');
- var mongodb = require('mongodb');
- var url = 'mongodb://'+ config.database.username+ ':' + config.database.password + '@' + config.database.host + ':' + config.database.port + '/' + config.database.name;
- var Database = function() {
- var db;
- // always return the singleton instance, if it has been initialised once already.
- if (Database.prototype._singletonInstance) {
- return Database.prototype._singletonInstance;
- }
- this.getDatabase = function() {
- return db;
- }
- this.get = this.getDatabase;
- mongodb.MongoClient.connect(url, function(err, result) {
- if(err || result === undefined || result === null) {
- throw err;
- } else {
- db = result;
- }
- });
- Database.prototype._singletonInstance = this;
- };
- new Database();
- console.log('MongoDb set up OK!');
- exports.Database = Database;
- };
- var express = require('express');
- var bodyParser = require('body-parser');
- var restful_express = express();
- restful_express.use(bodyParser());
- var allowCrossDomain = function(req, res, next) {
- res.header('Access-Control-Allow-Origin', '*');
- res.header('Access-Control-Allow-Methods', 'GET, POST');
- res.header('Access-Control-Expose-Headers', 'token');
- res.header('Access-Control-Allow-Headers', 'Content-Type, X-XSRF-TOKEN, Last-Event-ID, Content-Language, Accept-Language, Accept');
- res.header('X-XSS-Protection', 0);
- next();
- }
- restful_express.use(allowCrossDomain);
- restful_express.listen(process.env.PORT || 7272, function(err) {
- winlog.info('ExpressJs listening at 7272');
- });
- var Restful = function() {
- // always return the singleton instance, if it has been initialised once already.
- if (Restful.prototype._singletonInstance) {
- return Restful.prototype._singletonInstance;
- }
- this.getRestful = function() {
- return restful_express;
- }
- Restful.prototype._singletonInstance = this;
- };
- new Restful();
- console.log('Express.js Restful set up OK!');
- exports.Restful = Restful;
- var config = require('./config');
- var database = require('./database');
- var db = database.Database();
- var restful = require('./restful').Restful().getRestful();
- module.exports = function(){
- restful.get('/getUser/:email', function(req, res) {
- db.get().collection("user").findOne({email:req.param('email')}, function(err, result) {
- if(result) {
- res.send({name:result.name});
- } else {
- res.statusCode = 400;
- res.send({msg:'Email not found'});
- }
- });
- });
- });
- var config = require('./config');
- var database = require('./database');
- var db = database.Database();
- var restful = require('./restful').Restful().getRestful();
- module.exports = function(){
- restful.get('/getGroup/:name', function(req, res) {
- db.get().collection("group").findOne({name:req.param('name')}, function(err, result) {
- if(result) {
- res.send({name:result.name});
- } else {
- res.statusCode = 400;
- res.send({msg:'Group not found'});
- }
- });
- });
- });
- var config = require('./config');
- var mongodb = require('mongodb');
- var db;
- var url = 'mongodb://'+ config.database.username+ ':' + config.database.password + '@' + config.database.host + ':' + config.database.port + '/' + config.database.name;
- module.exports = function(){
- initDb : function () {
- mongodb.MongoClient.connect(url, function(err, result) {
- if(err || result === undefined || result === null) {
- throw err;
- } else {
- db = result;
- }
- });
- },
- getDb : function () {
- if(db === null || db === undefined) {
- this.initDb();
- }
- return db;
- }
- };
- //index.js
- var i = 0;
- i ++;
- exports.show = function(){
- return i;
- };
- exports.increment = function(){
- return i++;
- };
- > var one = require('./index.js');
- > one.show();
- 1
- > var two = require('./index.js');
- > two.show();
- 1
- > one.increment();
- 2
- > two.show();
- 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement