Advertisement
ralf52

inwx nodejs example app

Oct 26th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // to start use:
  2. // sudo pm2 start ~/nodejs_apps/inwx.json
  3. // to delete from pm2 use:
  4. // sudo pm2 delete inwx
  5. // to view logs:
  6. // sudo pm2 inwx
  7.  
  8. var express = require("express");
  9. var http = require('http'); // http server
  10.  
  11. var app = express();
  12. var bodyParser = require("body-parser");
  13. var request = require('request');
  14.  
  15.  
  16. app.use(bodyParser.urlencoded({ extended: false })); // parse application/x-www-form-urlencoded
  17. app.use(bodyParser.json()); // parse application/json
  18.  
  19. // https://github.com/mattes/inwx-nodejs
  20. var inwx = require('inwx');
  21.  
  22. app.use(function(req, res, next) {
  23.     log(req.protocol + ": " +  req.method + " " + req.originalUrl /*+ "\n body = " + JSON.stringify(req.body)*/); // do not log body because it can get huge
  24.     res.header('Access-Control-Allow-Origin', '*');
  25.     res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'); // OPTIONS?
  26.     res.header('Access-Control-Allow-Headers', 'Accept, Accept-CH, Accept-Charset, Accept-Datetime, Accept-Encoding, Accept-Ext, Accept-Features, Accept-Language, Accept-Params, Accept-Ranges, Access-Control-Allow-Credentials, Access-Control-Allow-Headers, Access-Control-Allow-Methods, Access-Control-Allow-Origin, Access-Control-Expose-Headers, Access-Control-Max-Age, Access-Control-Request-Headers, Access-Control-Request-Method, Age, Allow, Alternates, Authentication-Info, Authorization, C-Ext, C-Man, C-Opt, C-PEP, C-PEP-Info, CONNECT, Cache-Control, Compliance, Connection, Content-Base, Content-Disposition, Content-Encoding, Content-ID, Content-Language, Content-Length, Content-Location, Content-MD5, Content-Range, Content-Script-Type, Content-Security-Policy, Content-Style-Type, Content-Transfer-Encoding, Content-Type, Content-Version, Cookie, Cost, DAV, DELETE, DNT, DPR, Date, Default-Style, Delta-Base, Depth, Derived-From, Destination, Differential-ID, Digest, ETag, Expect, Expires, Ext, From, GET, GetProfile, HEAD, HTTP-date, Host, IM, If, If-Match, If-Modified-Since, If-None-Match, If-Range, If-Unmodified-Since, Keep-Alive, Label, Last-Event-ID, Last-Modified, Link, Location, Lock-Token, MIME-Version, Man, Max-Forwards, Media-Range, Message-ID, Meter, Negotiate, Non-Compliance, OPTION, OPTIONS, OWS, Opt, Optional, Ordering-Type, Origin, Overwrite, P3P, PEP, PICS-Label, POST, PUT, Pep-Info, Permanent, Position, Pragma, ProfileObject, Protocol, Protocol-Query, Protocol-Request, Proxy-Authenticate, Proxy-Authentication-Info, Proxy-Authorization, Proxy-Features, Proxy-Instruction, Public, RWS, Range, Referer, Refresh, Resolution-Hint, Resolver-Location, Retry-After, Safe, Sec-Websocket-Extensions, Sec-Websocket-Key, Sec-Websocket-Origin, Sec-Websocket-Protocol, Sec-Websocket-Version, Security-Scheme, Server, Set-Cookie, Set-Cookie2, SetProfile, SoapAction, Status, Status-URI, Strict-Transport-Security, SubOK, Subst, Surrogate-Capability, Surrogate-Control, TCN, TE, TRACE, Timeout, Title, Trailer, Transfer-Encoding, UA-Color, UA-Media, UA-Pixels, UA-Resolution, UA-Windowpixels, URI, Upgrade, User-Agent, Variant-Vary, Vary, Version, Via, Viewport-Width, WWW-Authenticate, Want-Digest, Warning, Width, X-Content-Duration, X-Content-Security-Policy, X-Content-Type-Options, X-CustomHeader, X-DNSPrefetch-Control, X-Forwarded-For, X-Forwarded-Port, X-Forwarded-Proto, X-Frame-Options, X-Modified, X-OTHER, X-PING, X-PINGOTHER, X-Powered-By, X-Requested-With');
  27.    
  28.     res.header("Content-Type", "application/json; charset=utf-8");
  29.    
  30.     next();
  31. });
  32.  
  33. app.get("/inwx", function (req, res) {
  34.     var rc = {};
  35.     rc.request = req.method + " " + req.originalUrl;
  36.     rc.request_body = req.body;
  37.    
  38.     // set api to production or testing
  39.     inwx({api: "production", user: "myusername", password: "mypassword"}, function(api){
  40.         log("API is ready"); // NEVER SHOWN!!!
  41.  
  42.         // get account infos
  43.         api.call("account", "info", {}, function(response){
  44.             var msg = "account.info response:" + response;
  45.            
  46.             rc.message = msg;
  47.             log(msg); // NEVER SHOWN!!!
  48.            
  49.             api.close();
  50.            
  51.             rc.status = 200;
  52.             res.status(200);
  53.             res.send(rc);
  54.         });
  55.  
  56.         // SOA serial numbers are updated by INWX automatically!  
  57.        
  58.    
  59.  
  60.         // be aware that calls are made asynchronously! call api.close() when all other calls terminated.
  61.         // api.close(); // logout
  62.     });
  63. });
  64.  
  65. app.options("*", function (req, res) {
  66.     log(req.method + " " + req.originalUrl);
  67.     res.send("GET, POST, PUT, DELETE");
  68. });
  69.  
  70.  
  71. var listenerHTTP = http.createServer(app).listen(6012, function () {
  72.     log("HTTP: app listening on port " + listenerHTTP.address().port + "!");
  73. });
  74.  
  75. function log(msg) {
  76.     var d = new Date();
  77.     console.log(d.toISOString() + " inwx: " + msg);
  78. }
  79.  
  80. function logerror(msg) {
  81.     var d = new Date();
  82.     console.error(d.toISOString() + " inwx: Error! " + msg);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement