Advertisement
Guest User

Untitled

a guest
Mar 17th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. var loopback = require('loopback');
  2. var boot = require('loopback-boot');
  3. var cfenv = require('cfenv');
  4. var bodyParser = require("body-parser");
  5. var cookieParser = require('cookie-parser');
  6. require('body-parser-xml')(bodyParser);
  7.  
  8. var app = module.exports = loopback();
  9.  
  10. var appEnv = cfenv.getAppEnv();
  11.  
  12. app.use(bodyParser.json());
  13. app.use(bodyParser.xml({
  14. limit: '1MB', // Reject payload bigger than 1 MB
  15. xmlParseOptions: {
  16. normalize: true, // Trim whitespace inside text nodes
  17. normalizeTags: false, // Transform tags to lowercase
  18. explicitArray: false // Only put nodes in array if >1
  19. }
  20. }));
  21.  
  22. app.use(bodyParser.urlencoded({
  23. "extended" : true
  24. }));
  25.  
  26.  
  27.  
  28. // boot scripts mount components like REST API
  29. boot(app, __dirname);
  30.  
  31. app.start = function() {
  32. // start the web server
  33. return app.listen(process.env.PORT || 3000, function() {
  34. console.log("env port"+process.env.PORT);
  35. app.emit('started');
  36. var baseUrl = app.get('url').replace(//$/, '');
  37. console.log('Web server listening at: %s', baseUrl);
  38. if (app.get('loopback-component-explorer')) {
  39. var explorerPath = app.get('loopback-component-explorer').mountPath;
  40. console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
  41. }
  42. });
  43. };
  44.  
  45. // start the server if `$ node server.js`
  46. if (require.main === module) {
  47. app.start();
  48. }
  49.  
  50. module.exports = function(app) {
  51. var router = app.loopback.Router();
  52. var User = app.models.pusers;
  53. var js2xmlparser = require("js2xmlparser");
  54.  
  55. app.use(function (req, res, next) {
  56. res.header("Access-Control-Allow-Origin", "*");
  57. res.header("Access-Control-Allow-Headers", "X-Requested-With");
  58. if(req.get("content-type") !== 'undefined'){
  59. if(req.get("content-type") == 'application/json'){
  60. res.setHeader('content-type', req.get("content-type"));
  61. }else if(req.get("content-type") == 'application/xml'){
  62. res.setHeader('content-type', req.get("content-type"));
  63. }
  64. }
  65. next();
  66. });
  67.  
  68. app.middleware('initial', function logResponse(req, res, next) {
  69. res.on('finish', function() {
  70. });
  71. req.on('end', function(data) {
  72. });
  73. req.on('data', function(data) {
  74. // the request was handled, print the log entry
  75. console.log(req.method, req.originalUrl, res.statusCode);
  76. if(req.get("content-type") == 'application/xml'){
  77. console.log("xml data's :"+data);
  78. }else if(req.get("content-type") == 'application/json'){
  79. console.log("json data's :"+data);
  80. }
  81. });
  82. next();
  83. });
  84.  
  85. function responseHandler(req,res,data){
  86. if(req.get("content-type") == 'application/json'){
  87. return JSON.stringify(data);
  88. }else if(req.get("content-type") == 'application/xml'){
  89. return js2xmlparser("response", JSON.stringify(data));
  90. }else
  91. return data;
  92. }
  93.  
  94. router.post('/login', function(req, res){
  95. var response = {};
  96. console.log(req.body);
  97. console.log(req.body.username);
  98. try{
  99. User.find({where: {name: req.body.username}}, function(err, data) {
  100. if (err) {
  101. response = {
  102. "error" : true,
  103. "message" : "Error fetching data"
  104. };
  105. } else {
  106. if(data.length != 0){
  107. if(data[0].name == req.body.username && data[0].password == req.body.password){
  108. response = {
  109. "error" : false,
  110. "data" : "Success"
  111. };
  112. }else {
  113. response = {
  114. "error" : false,
  115. "data" : "Password is incorrect"
  116. };
  117. }
  118. }else if(data.length == 0){
  119. response = {
  120. "error" : false,
  121. "data" : "Username is incorrect"
  122. };
  123. }
  124. }
  125. console.log("Check login");
  126. res.end(responseHandler(req,res,response));
  127. });
  128. }catch(ex){
  129. console.error("Error while retrive all data from User Model by name",ex);
  130. res.end(responseHandler(req,res,"Error while inserting User Model by name"));
  131. }
  132. });
  133.  
  134.  
  135.  
  136.  
  137. app.use(router);
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement