Skip to content

Commit

Permalink
Add Procfile for heroku and rename files
Browse files Browse the repository at this point in the history
- add .env to .gitignore
- read AWS S3 credentials from .env
- update redis store config for heroku redisToGo adds on
- remove unused code
  • Loading branch information
purnimagupta committed Oct 10, 2019
1 parent 8c2d903 commit 9d37461
Show file tree
Hide file tree
Showing 16 changed files with 137 additions and 216 deletions.
3 changes: 0 additions & 3 deletions .env

This file was deleted.

2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ node_modules/
/src/config.ts
lib
data

.env
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: npm start
41 changes: 22 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 18 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,30 @@
"name": "reduxtagram-server",
"version": "0.0.0",
"private": true,
"engines": {
"node": "10.15.3",
"npm": "6.4.1"
},
"scripts": {
"start": "npm run build:live",
"start-dev": "NODE_ENV=development npm run build:live",
"start-prod": "NODE_ENV=production npm run build:live",
"clean": "rimraf ./lib/",
"start": "npm-run-all clean --parallel watch:server --print-label",
"tsc": "tsc",
"build": "tsc -b",
"build:live": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/server.ts"
"watch:build": "tsc --watch",
"watch:server": "nodemon --watch 'src/**/*.ts' --exec ./node_modules/.bin/ts-node src/server.ts",
"postinstall": "./node_modules/.bin/tsc ./tsconfig.json || echo done"
},
"dependencies": {
"@types/aws-sdk": "^2.7.0",
"@types/cookie-parser": "^1.4.2",
"@types/express": "^4.17.1",
"@types/express-session": "^1.15.14",
"@types/mime": "^2.0.1",
"@types/mongodb": "^3.3.1",
"@types/mongoose": "^5.5.17",
"@types/mongoose-delete": "^0.5.0",
"@types/multer": "^1.3.9",
"@types/multer-s3": "^2.7.7",
"@types/node": "^12.7.2",
"aws-sdk": "^2.529.0",
"bcrypt": "^3.0.6",
"connect-redis": "^3.4.2",
Expand All @@ -36,17 +45,12 @@
"multer-s3": "^2.9.0",
"nodemon": "^1.19.3",
"npm-run-all": "^4.1.5",
"redis": "^2.8.0",
"rimraf": "^3.0.0",
"ts-node": "^8.3.0",
"typescript": "^3.6.3",
"typings": "^2.1.1",
"url": "^0.11.0",
"validator": "^11.1.0"
},
"devDependencies": {
"@types/express": "^4.17.1",
"@types/mongodb": "^3.3.1",
"@types/mongoose": "^5.5.17",
"@types/mongoose-delete": "^0.5.0",
"@types/node": "^12.7.2",
"ts-node": "^8.3.0",
"typescript": "^3.6.3"
}
}
104 changes: 59 additions & 45 deletions src/app1.ts → src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ import Controller from './interfaces/controller.interface';
import HttpException from './exceptions/HttpException';
const cors = require('cors');
const logger = require('morgan');
const redis = require('redis');
const session = require('express-session');
const RedisStore = require('connect-redis')(session);

// import errorMiddleware from './middleware/error.middleware';
const url = require('url');


let sessionStore;

class App {
public app: express.Application;
Expand All @@ -23,33 +27,58 @@ class App {
this.initializeMiddlewares();
this.initializeControllers(controllers);
this.initializeErrorHandling();
// this.acceptAllRoutes();
}

private initializeMiddlewares() {
this.app.use(express.urlencoded({ extended: false }));
this.app.use(bodyParser.json());

this.app.use(cors({
const options= {
allowedHeaders: ["Origin", "X-Requested-With", "Content-Type", "Accept", "X-Access-Token"],
credentials: true,
origin: "http://localhost:3000"
}));
methods: "GET,HEAD,OPTIONS,PUT,PATCH,POST,DELETE",
origin: "https://desolate-stream-98688.herokuapp.com/",
preflightContinue: false
};

if(process.env.NODE_ENV === "production") {
this.app.use(cors(options))
}
else {
this.app.use(cors({
credentials: true,
origin: "http://localhost:3000"
}));
}

this.app.use(express.json());
// view engine setup
this.app.set('views', path.join(__dirname, 'views'));
this.app.set('view engine', 'jade');

this.app.use(logger('dev'));
this.app.use(express.urlencoded({ extended: true }));
this.app.use(cookieParser());

this.app.use(express.static(path.join(__dirname, 'public')));
this.app.use(bodyParser.json());

let sessionStore = new RedisStore({
host: process.env.REDISIP,
port: 6379,
db: 2,
ttl: 60 * 60 * 24 * 365
});
// Serve static files from the React app
// this.app.use(express.static(path.join(__dirname, '../client/build')));

if(process.env.NODE_ENV === 'production') {
const rtg = url.parse(process.env.REDISTOGO_URL);
const redisClient = redis.createClient(rtg.port, rtg.hostname);

redisClient.auth(rtg.auth.split(":")[1]);

sessionStore = new RedisStore({
redisClient
})
}
else {
const redisClient = redis.createClient();
sessionStore = new RedisStore({
redisClient
})
}

this.app.use(session({
name: 'purnima',
Expand All @@ -58,38 +87,19 @@ class App {
resave: false,
saveUninitialized: false,
cookie: {
secure: false,
sameSite: false,
secure: process.env.NODE_ENV === 'production'? true:false,
sameSite: process.env.NODE_ENV === 'production'? true:false,
maxAge: 36000000,
httpOnly: false
// domain: "http://localhost:3000"
httpOnly: false,
// domain: "https://desolate-stream-98688.herokuapp.com/"
}
}));

// // catch 404 and forward to error handler
// this.app.use(function(req: Request, res: Response, next: NextFunction) {
// next(createError(404));
// });

// error handler
// this.app.use(function(err:any, req: Request, res: Response, next: NextFunction) {
// // res.header("Access-Control-Allow-Origin", "http://localhost:3000");
// // res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
// // next();
// console.log("error handleer.......................................................")
// // set locals, only providing error in development
// res.locals.message = err.message;
// res.locals.error = req.app.get('env') === 'development' ? err : {};

// // render the error page
// res.status(err.status || 500)
// // res.render('error');
// });

}

public listen() {
this.app.listen(process.env.PORT, () => {
const PORT = process.env.PORT || 5000;
this.app.listen(PORT, () => {
console.log(`App listening on the port ${process.env.PORT}`);
})
}
Expand All @@ -99,7 +109,11 @@ class App {
this.app.use('/', controller.router)
})
}

private acceptAllRoutes() {
this.app.get('*', (req, res) => {
res.sendFile(path.join(__dirname+'../client/build/index.html'));
});
}
private initializeErrorHandling() {
this.app.use(function(error:HttpException, req: Request, res: Response, next: NextFunction) {

Expand All @@ -117,14 +131,14 @@ class App {

//Connect to database
private connectToTheDatabase() {
let dbURL = `mongodb://${process.env.MONGOIP}:27017/reduxtagram-server`;
const { MONGODB_URL } = process.env
mongoose.connect(dbURL, { useNewUrlParser: true}, function(err: any){
let mongodb_url =process.env.NODE_ENV === "production" ? process.env.MONGOIP : `mongodb://${process.env.MONGOIP}:27017/reduxtagram-server`
console.log(mongodb_url)
mongoose.connect(mongodb_url, { useNewUrlParser: true}, function(err: any){
if(err) {
console.log('Error connecting...: ', dbURL)
console.log('Error connecting..database ')
}
else {
console.log('Connected to mongodb : ' + dbURL)
console.log('Connected to mongodb')
}
})
}
Expand Down
Loading

0 comments on commit 9d37461

Please sign in to comment.