Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. require('./restful');
  2. require('./database');
  3.  
  4. module.exports = function(){
  5.  
  6. database :{
  7. username: "mark",
  8. password: "pass",
  9. host: "127.0.0.1",
  10. port: "27017",
  11. name: "mydb"
  12. };
  13.  
  14. var config = require('./config');
  15. var mongodb = require('mongodb');
  16.  
  17. var url = 'mongodb://'+ config.database.username+ ':' + config.database.password + '@' + config.database.host + ':' + config.database.port + '/' + config.database.name;
  18.  
  19. var Database = function() {
  20. var db;
  21.  
  22. // always return the singleton instance, if it has been initialised once already.
  23. if (Database.prototype._singletonInstance) {
  24. return Database.prototype._singletonInstance;
  25. }
  26.  
  27. this.getDatabase = function() {
  28. return db;
  29. }
  30.  
  31. this.get = this.getDatabase;
  32.  
  33. mongodb.MongoClient.connect(url, function(err, result) {
  34. if(err || result === undefined || result === null) {
  35. throw err;
  36. } else {
  37. db = result;
  38. }
  39. });
  40.  
  41. Database.prototype._singletonInstance = this;
  42. };
  43.  
  44. new Database();
  45.  
  46. console.log('MongoDb set up OK!');
  47.  
  48. exports.Database = Database;
  49. };
  50.  
  51. var express = require('express');
  52. var bodyParser = require('body-parser');
  53. var restful_express = express();
  54. restful_express.use(bodyParser());
  55.  
  56. var allowCrossDomain = function(req, res, next) {
  57. res.header('Access-Control-Allow-Origin', '*');
  58. res.header('Access-Control-Allow-Methods', 'GET, POST');
  59. res.header('Access-Control-Expose-Headers', 'token');
  60. res.header('Access-Control-Allow-Headers', 'Content-Type, X-XSRF-TOKEN, Last-Event-ID, Content-Language, Accept-Language, Accept');
  61. res.header('X-XSS-Protection', 0);
  62. next();
  63. }
  64.  
  65. restful_express.use(allowCrossDomain);
  66.  
  67. restful_express.listen(process.env.PORT || 7272, function(err) {
  68. winlog.info('ExpressJs listening at 7272');
  69. });
  70.  
  71. var Restful = function() {
  72.  
  73. // always return the singleton instance, if it has been initialised once already.
  74. if (Restful.prototype._singletonInstance) {
  75. return Restful.prototype._singletonInstance;
  76. }
  77.  
  78. this.getRestful = function() {
  79. return restful_express;
  80. }
  81.  
  82. Restful.prototype._singletonInstance = this;
  83. };
  84.  
  85. new Restful();
  86.  
  87. console.log('Express.js Restful set up OK!');
  88.  
  89. exports.Restful = Restful;
  90.  
  91. var config = require('./config');
  92. var database = require('./database');
  93. var db = database.Database();
  94. var restful = require('./restful').Restful().getRestful();
  95.  
  96. module.exports = function(){
  97.  
  98. restful.get('/getUser/:email', function(req, res) {
  99.  
  100. db.get().collection("user").findOne({email:req.param('email')}, function(err, result) {
  101.  
  102. if(result) {
  103. res.send({name:result.name});
  104. } else {
  105. res.statusCode = 400;
  106. res.send({msg:'Email not found'});
  107. }
  108. });
  109. });
  110. });
  111.  
  112. var config = require('./config');
  113. var database = require('./database');
  114. var db = database.Database();
  115. var restful = require('./restful').Restful().getRestful();
  116.  
  117. module.exports = function(){
  118.  
  119. restful.get('/getGroup/:name', function(req, res) {
  120.  
  121. db.get().collection("group").findOne({name:req.param('name')}, function(err, result) {
  122.  
  123. if(result) {
  124. res.send({name:result.name});
  125. } else {
  126. res.statusCode = 400;
  127. res.send({msg:'Group not found'});
  128. }
  129. });
  130. });
  131.  
  132. });
  133.  
  134. var config = require('./config');
  135. var mongodb = require('mongodb');
  136. var db;
  137. var url = 'mongodb://'+ config.database.username+ ':' + config.database.password + '@' + config.database.host + ':' + config.database.port + '/' + config.database.name;
  138.  
  139. module.exports = function(){
  140. initDb : function () {
  141. mongodb.MongoClient.connect(url, function(err, result) {
  142. if(err || result === undefined || result === null) {
  143. throw err;
  144. } else {
  145. db = result;
  146. }
  147. });
  148. },
  149.  
  150. getDb : function () {
  151. if(db === null || db === undefined) {
  152. this.initDb();
  153. }
  154. return db;
  155. }
  156. };
  157.  
  158. //index.js
  159. var i = 0;
  160. i ++;
  161. exports.show = function(){
  162. return i;
  163. };
  164. exports.increment = function(){
  165. return i++;
  166. };
  167.  
  168. > var one = require('./index.js');
  169. > one.show();
  170. 1
  171. > var two = require('./index.js');
  172. > two.show();
  173. 1
  174. > one.increment();
  175. 2
  176. > two.show();
  177. 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement