· 9 years ago · Nov 18, 2016, 06:54 PM
12016/11/18 18:41:13 [error] 10689#0: *28 connect() failed (111: Connection refused) while connecting to upstream, client: 92.169.74.116, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "www.mydomain.io"
2
3
4Error: Cannot find module '/var/app/current/app.js'
5 at Function.Module._resolveFilename (module.js:469:15)
6 at Function.Module._load (module.js:417:25)
7 at Module.runMain (module.js:604:10)
8 at run (bootstrap_node.js:394:7)
9 at startup (bootstrap_node.js:149:9)
10 at bootstrap_node.js:509:3
11
12import * as express from "express"
13import * as helmet from "helmet"
14import users from "./src/users/boundaries-users/users-boundaries"
15import * as directory from "./src/directory/boudaries-directory/links-boudaries"
16import * as comment from "./src/comments/boundaries-comments/comments-boundaries"
17import admin from "./src/@admin/boundaries-admin/admin-boundaries"
18import * as session from "express-session"
19import Home from "./src/home/use-cases-home/home-case"
20import * as email from "./src/utilities-global/email"
21import * as csrf from "csurf"
22let nodemailer = require( "nodemailer" )
23import * as utils from "./src/utilities-global/utilities"
24let RateLimit = require( "express-rate-limit" )
25
26let app = express()
27app.set( "port", process.env.PORT || 8081 )
28// app.use( helmet() )
29app.locals.moment = require( "moment" )
30app.locals.truncate = require( "lodash/truncate" )
31app.locals.split = require( "lodash/split" )
32
33
34/* ---------------------------------------------------------------------------------------------------------------*/
35// Rate limitation
36/* ---------------------------------------------------------------------------------------------------------------*/
37
38app.enable( "trust proxy" )
39const limiter = new RateLimit( {
40 "windowMs": 1000 * 10, // In milliseconds
41 "max" : 100, // limit each IP to XX requests per windowMs
42 "delayMs" : 0 // disable delaying - full speed until the max limit is reached
43} )
44
45app.use( limiter )
46
47
48/* ---------------------------------------------------------------------------------------------------------------*/
49// Sessions
50/* ---------------------------------------------------------------------------------------------------------------*/
51app.use( session( {
52 "secret" : "**********",
53 "resave" : false, // Default true
54 "saveUninitialized": false, // default true
55 "cookie" : {
56 "secure": false,
57 "maxAge": utils.durationInMilliseconds.oneDay * 7
58 }
59} ) )
60
61// Middleware used to allow template system to acces session information. Must be placed between session and routes middlewares.
62// http://expressjs.com/fr/api.html#res.locals and
63// http://stackoverflow.com/questions/16823008/express-res-locals-somevariable-use-in-hbs-handlebars-template
64app.use( function( req, res, next ) {
65 res.locals.session = req.session
66 next()
67} )
68
69// Console log sessions
70//app.use( function( req, res, next ) {
71// console.log( req.session)
72// next()
73//} )
74
75
76/* ---------------------------------------------------------------------------------------------------------------*/
77// Csrf Protection Middleware
78/* ---------------------------------------------------------------------------------------------------------------*/
79app.use( csrf() )
80// We put the token in locals so it is easily accessible from any template. No need to pass it as a new param
81// to the boundaries.
82app.use( function( req, res, next ) {
83 res.locals._csrf = req.csrfToken()
84 next()
85} )
86
87
88/* ---------------------------------------------------------------------------------------------------------------*/
89// Home url handling
90/* ---------------------------------------------------------------------------------------------------------------*/
91
92// Home
93app.get( "/", ( req, res ) => {
94 const url = "/"
95
96 new Home( req, res ).homeDisplay( undefined, undefined, undefined, "isHome", url )
97} )
98
99// Home searched
100app.get( "/from/:source/to/:target/:sorting", ( req, res ) => {
101 const sourceLanguage = req.params.source
102 const targetLanguage = req.params.target
103 const sorting = req.params.sorting
104 const url = req.url
105
106 new Home( req, res ).homeDisplay( sourceLanguage, targetLanguage, sorting, "isNotHome", url )
107} )
108
109// Contact
110app.get( "/contact", ( req, res ) => {
111
112 res.render( "contact.ejs", {
113 "data": {
114 "pageTitle": "Contact us"
115 }
116 } )
117} )
118
119
120/* ---------------------------------------------------------------------------------------------------------------*/
121// Routes
122/* ---------------------------------------------------------------------------------------------------------------*/
123app.use( "/users", users )
124app.use( "/directory", directory.routerDirectory )
125app.use( "/comment", comment.routerComments )
126app.use( "/admin", admin )
127
128
129/** Views */
130app.set( "views", [
131 __dirname + "/src/users/views-users",
132 __dirname + "/src/directory/views-directory",
133 __dirname + "/src/comments/views-comments",
134 __dirname + "/src/views-global",
135 __dirname + "/src/@admin/views-admin",
136 __dirname + "/src/home/views-home"
137] )
138
139app.set( "view engine", "ejs" )
140
141
142/* ---------------------------------------------------------------------------------------------------------------*/
143// Static handling
144/* ---------------------------------------------------------------------------------------------------------------*/
145app.use( express.static( __dirname + "/src/@frontend" ) )
146
147
148/* ---------------------------------------------------------------------------------------------------------------*/
149// Error Handlers
150/* ---------------------------------------------------------------------------------------------------------------*/
151
152// Handle uncaught exception
153process.on( "uncaughtException", function( er ) {
154 const transporter = nodemailer.createTransport( email.smtpConfig )
155 console.error( er.stack ) // [3]
156 transporter.sendMail( {
157 "from" : "alerts@$$$$$$$.com",
158 "to" : "$$$$$$$@gmail.com",
159 "subject": er.message,
160 "text" : er.stack
161 }, function( er ) {
162 if ( er ) {
163 console.error( er )
164 }
165 process.exit( 1 )
166 } )
167} )
168
169
170// Handle crsf error
171app.use( function( err, req, res, next ) {
172 if ( err.code !== "EBADCSRFTOKEN" ) {
173 return next( err )
174 }
175
176 // handle CSRF token errors here
177 res.status( 403 )
178 res.send( "form tampered with" )
179} )
180
181
182/* ---------------------------------------------------------------------------------------------------------------*/
183// Server Start
184/* ---------------------------------------------------------------------------------------------------------------*/
185app.listen( app.get( "port" ), function() {
186 console.log( "Example app listening on port: " + app.get( "port" ))
187} )