var express = require("express"), swagger = require("./swagger"), orm = require('./orm'), auth = require('./auth'), config = require('./config'), static_files = require('./static'); var app = express(); app.use(express.bodyParser()); auth.init(app); orm.init(app); swagger(app); static_files(app); app.listen(config.listen, config.ipaddr);
var client = new memcache.Client(cfg.memcache.port, cfg.memcache.host); ... app.use(function(req, res, next){ client.get( req.headers[cfg.header.toLowerCase()], function(error, result){ if(result){ req.user = result; } next(); } ); req.memcache = client; });
app.use(orm.express(config.db, { define: function (db, models) { db.define("users", { id : Number, email : String, password : String, twitter : Number, facebook : Number, google : Number, linkedin : String }, { validations: { email: [ orm.enforce.unique("Email already taken!"), orm.enforce.unique({ ignoreCase: true }), orm.enforce.notEmptyString() ], password: orm.enforce.notEmptyString() }, id: "id", autoFetch: false } ); var Conferences = db.define("conferences", { id : Number, title : String, description : String, datetime : Date, place : String, location : String, site : String, logo : String, facebook : String, twitter : String, telephone : String, cost : String, file : String },{ id: "id", autoFetch: false } ); var Decisions = db.define("decisions", { id : Number, decision : ['go', 'not go', 'favorite'], user : Number, conference_id : Number },{ id: "id", cache: false, autoFetch: false } ); Decisions.hasOne('conference', Conferences, {reverse: 'decision'}); } }));
var static_handler = express.static(__dirname + '/../static/'); app.get(/^\/static(\/.*)?$/, function(req, res, next) { if (req.url === '/static') { // express static barfs on root url w/o trailing slash res.writeHead(302, { 'Location' : req.url + '/' }); res.end(); return; } req.url = req.url.substr('/static'.length); return static_handler(req, res, next); }); var main_handler = express.static(__dirname + '/../client/www/'); app.get(/^\/(.*)$/, function(req, res, next) { if(req.url == '/cordova.js'){ return res.send(''); } if(!req.url){ req.url = 'index.html'; } return main_handler(req, res, next); });
app.use(function(req, res, next) { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); res.header('Access-Control-Allow-Headers', cfg.header+', Content-Type'); res.header('Access-Control-Expose-Headers', cfg.header+', Content-Type'); if (req.method == 'OPTIONS') { res.send(200); } else { next(); } }); swagger.setAppHandler(app); swagger.addModels(models); controllers.init(swagger); swagger.configure(cfg.basePath, "0.1"); // Serve up swagger ui at /docs via static route var docs_handler = express.static(__dirname + '/../documentation/swagger/'); app.get(/^\/docs(\/.*)?$/, function(req, res, next) { if (req.url === '/docs') { // express static barfs on root url w/o trailing slash res.writeHead(302, { 'Location' : req.url + '/' }); res.end(); return; } // take off leading /docs so that connect locates file correctly req.url = req.url.substr('/docs'.length); return docs_handler(req, res, next); });
swagger.addModels(models); controllers.init(swagger);
swagger.addGET(conferences.get); swagger.addGET(conferences.list); swagger.addPOST(conferences.decision); swagger.addDELETE(conferences.reject);
var get = { 'spec': { "description" : "Get conference by id", "path" : "/conferences.{format}/{id}", "notes" : "Get conference", "summary" : "Get conference", "method": "GET", "responseClass" : "Conference", "nickname" : "conference" }, 'action': function (req,res) { if (!req.params.id) { throw swagger.errors.invalid('id'); } var id = parseInt(req.params.id); req.db.models.conferences.get(id, function(err, conference){ if(err){ res.send(500, JSON.stringify({code: 500, header: 'Internal Server Error', message: JSON.stringify(err)})); }else{ if(conference){ if(conference.file){ conference.file = '/static/' + conference.file; } if(req.user){ conference.getDecision({user: req.user}, function(err, decision){ if(err){ res.send(500, JSON.stringify(err)); }else{ conference.decision = decision.pop(); res.send(200, JSON.stringify(conference)); } }); }else{ res.send(200, JSON.stringify(conference)); } }else{ throw swagger.errors.notFound('conference'); } } }); } };
var static_handler = express.static(__dirname + '/www/static/'); app.get(/^\/static(\/.*)?$/, function(req, res, next) { if (req.url === '/static') { // express static barfs on root url w/o trailing slash res.writeHead(302, { 'Location' : req.url + '/' }); res.end(); return; } // take off leading /docs so that connect locates file correctly req.url = req.url.substr('/docs'.length); return static_handler(req, res, next); }); var main_handler = express.static(__dirname + '/www/'); app.get(/^\/(.*)$/, function(req, res, next) { if(req.url == '/cordova.js'){ return res.send(''); } if(!req.url){ req.url = 'index.html'; } return main_handler(req, res, next); });
git add --all; git commit -m 'commit'; git push origin master
exports.listen = process.env.OPENSHIFT_NODEJS_PORT || 8080; exports.ipaddr = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
echo $OPENSHIFT_POSTGRESQL_DB_URL
Source: https://habr.com/ru/post/200356/
All Articles