Skip to content

Commit

Permalink
Merge pull request #131 from Ignitus/develop
Browse files Browse the repository at this point in the history
TS Support added ★
  • Loading branch information
divyanshu-rawat authored Mar 21, 2020
2 parents e901156 + f783d14 commit 3b95b8e
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 54 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules/*
package-lock.json
build/
.idea/*
package.json
/coverage
Expand Down
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: npm start
9 changes: 9 additions & 0 deletions api/Types/customError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class CustomError extends Error {
public name: string;
public status: number;
public constructor(message: string, status: number) {
super(message);
this.name = 'CustomError';
this.status = status;
}
}
46 changes: 24 additions & 22 deletions index.js → api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import express from 'express';
import path from 'path';

/* for additional logging. */
import logger from 'morgan';
import logger from 'morgan';

/* for push notification. */
import webPush from 'web-push';
Expand All @@ -14,18 +14,23 @@ import cookieParser from 'cookie-parser';
import bodyParser from 'body-parser';

/* db connection/envs. */
import connectDB from './api/Configuration/db.js';
import { config } from './api/Configuration/config.js';
import connectDB from './Configuration/db.js';
import { config } from './Configuration/config.js';

/* application routes. */
import userRouter from './api/Routes/usersRouter.js';
import opportunityRouter from './api/Routes/opportunityRouter.js';
import studentRouter from './api/Routes/studentRouter.js';
import professorRouter from './api/Routes/professorRouter.js';
import testimonialRouter from './api/Routes/testimonialRouter.js';
import userRouter from './Routes/usersRouter.js';
import opportunityRouter from './Routes/opportunityRouter.js';
import studentRouter from './Routes/studentRouter.js';
import professorRouter from './Routes/professorRouter.js';
import testimonialRouter from './Routes/testimonialRouter.js';
// import teamMembersrouter from './api/Routes/teamMembersrouter.js';
import { CustomError } from './Types/customError';

webPush.setVapidDetails(`mailto:${config.privateVapidEmail}`, config.publicVapidKey, config.privateVapidKey);
webPush.setVapidDetails(
`mailto:${config.privateVapidEmail}`,
config.publicVapidKey || '',
config.privateVapidKey || ''
);

const app = express();
app.use(logger('dev'));
Expand All @@ -39,24 +44,24 @@ app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept',
'Origin, X-Requested-With, Content-Type, Accept'
);
next();
});

app.post('/subscribe', (req, res) => {
const subscription = req.body;
const payload: string = JSON.stringify({ title: 'Greetings by Igntius!' });
res.status(200).json({});
const payload = JSON.stringify({ title: 'Greetings by Igntius!' });
webpush
webPush
.sendNotification(subscription, payload)
.then(() => {
console.log(
'sendNotification success',
JSON.stringify({ title: 'Greetings by Igntius!' }),
JSON.stringify({ title: 'Greetings by Igntius!' })
);
})
.catch((error) => {
.catch((error: Error) => {
console.error('sendNotification ERROR', error.stack);
});
});
Expand All @@ -66,25 +71,22 @@ app.use('/', professorRouter);
app.use('/', userRouter);
app.use('/', opportunityRouter);
app.use('/', testimonialRouter);
// app.use('/', teamMembersrouter);

const PORT = process.env.PORT || 3000;
const PORT = process.env.PORT ?? 3000;
connectDB()
.then(() => {
app.listen(PORT, () => console.log(`Our app is running on port ${PORT}`));
})
.catch((err) => {
console.error('App starting error:', err.stack);
.catch((err: Error) => {
process.exit(1);
});

app.use((req, res, next) => {
const err = new Error('Not Found');
err.status = 404;
app.use((req, res, next): void => {
const err: CustomError = new CustomError('Not Found', 604);
next(err);
});

app.use((err, req, res) => {
app.use((err: any, req: any, res: any): void => {
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.status(err.status || 500);
Expand Down
6 changes: 0 additions & 6 deletions api/views/error.pug

This file was deleted.

5 changes: 0 additions & 5 deletions api/views/index.pug

This file was deleted.

7 changes: 0 additions & 7 deletions api/views/layout.pug

This file was deleted.

13 changes: 5 additions & 8 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
{
"watch": ["api"],
"ext": "js,json",
"ignore": ["*.test.js"],
"delay": "3",
"execMap": {
"ts": "ts-node"
}
}
"watch": ["api/"],
"ext": "ts, js",
"ignore": ["public"],
"exec": "NODE_ENV=development ts-node api/index.ts"
}
21 changes: 15 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
"name": "ignitus-server",
"version": "1.0.0",
"private": true,
"type": "module",
"engines": {
"node": "13.2.0"
},
"scripts": {
"start": "node --experimental-modules index.js",
"start-dev": "nodemon --watch --experimental-modules index.js --watch api",
"lint": "eslint ./ --ext .js",
"lint:fix": "eslint ./ --ext .js --fix"
"build": "tsc",
"postinstall": "npm run build",
"start": "ts-node build/index.js",
"start-dev": "nodemon",
"tslint": "tslint -c tslint.json -p tsconfig.json"
},
"nyc": {
"check-coverage": false,
Expand Down Expand Up @@ -40,6 +40,11 @@
"instrument": true
},
"dependencies": {
"@types/cookie-parser": "1.4.2",
"@types/express": "4.17.3",
"@types/morgan": "1.9.0",
"@types/node": "13.9.1",
"@types/web-push": "3.3.0",
"babel-register": "^6.26.0",
"bcrypt": "^3.0.1",
"bcryptjs": "^2.4.3",
Expand All @@ -54,12 +59,16 @@
"node-linkedin": "^0.5.6",
"nyc": "^15.0.0",
"redis": "^2.8.0",
"ts-node": "8.7.0",
"tslib": "1.11.1",
"typescript": "3.8.3",
"web-push": "^3.3.3"
},
"devDependencies": {
"eslint": "^5.6.1",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.14.0",
"nodemon": "^1.18.3"
"nodemon": "1.19.4",
"tslint": "6.1.0"
}
}
20 changes: 20 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"module": "commonjs",
"strict": true,
"baseUrl": "./",
"outDir": "build",
"allowJs": true,
"removeComments": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"target": "es6",
"emitDecoratorMetadata": true,
"moduleResolution": "node",
"importHelpers": true,
"types": ["node"],
"typeRoots": ["node_modules/@types"]
},
"include": ["api/**/*"],
"exclude": ["./src/public/"]
}
14 changes: 14 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "tslint:recommended",
"rules": {
"max-line-length": {
"options": [100]
},
"member-ordering": false,
"no-consecutive-blank-lines": false,
"object-literal-sort-keys": false,
"ordered-imports": false,
"quotemark": [true, "single"],
"variable-name": [true, "allow-leading-underscore"]
}
}

0 comments on commit 3b95b8e

Please sign in to comment.