Skip to content

Instantly share code, notes, and snippets.

@bbachi
bbachi / nodemon.json
Last active February 15, 2019 03:41
How to write production ready Node.js Rest API - Typescript version-version
{
"ignore": [".git", "node_modules"],
"watch": ["./"],
"exec": "npm start",
"ext": "ts"
}
@bbachi
bbachi / app.ts
Created February 15, 2019 04:36
How to write production ready Node.js Rest API - Typescript version-version
import * as express from 'express';
import * as bodyParser from 'body-parser';
import { Logger } from './logger/logger'
class App {
public express: express.Application;
public logger: Logger;
// rest of the file are left for brevity
@bbachi
bbachi / app.ts
Created February 15, 2019 04:58
typescript version
// left for brevity
// handle undefined routes
this.express.use('*', (req,res,next) => {
res.send("Make sure url is correct!!!");
});
@bbachi
bbachi / app.ts
Created February 15, 2019 05:38
How to define Context Path - Typescript version
import * as express from 'express';
import * as bodyParser from 'body-parser';
import { Logger } from './logger/logger';
import Routes from './routes/routes';
class App {
public express: express.Application;
public logger: Logger;
@bbachi
bbachi / tsconfig.json
Created February 15, 2019 06:07
tslint typescript version
{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es5",
"allowJs": true
},
"exclude":[
@bbachi
bbachi / package.json
Created February 15, 2019 06:14
final package.json
{
"name": "user_api",
"version": "1.0.0",
"description": "sample rest api",
"main": "index.ts",
"scripts": {
"start": "ts-node index.ts",
"start:dev": "./node_modules/nodemon/bin/nodemon.js",
"test": "echo \"Error: no test specified\" && exit 1",
"tslint": "tslint --project tsconfig.json --config tslint.json",
@bbachi
bbachi / package.json
Created February 15, 2019 06:14
final package.json
{
"name": "user_api",
"version": "1.0.0",
"description": "sample rest api",
"main": "index.ts",
"scripts": {
"start": "ts-node index.ts",
"start:dev": "./node_modules/nodemon/bin/nodemon.js",
"test": "echo \"Error: no test specified\" && exit 1",
"tslint": "tslint --project tsconfig.json --config tslint.json",
@bbachi
bbachi / Dockerfile
Created February 15, 2019 14:47
dockerfile -typescript version
# from base image node
FROM node:8.11-slim
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# copying all the files from your file system to container file system
COPY package.json .
# install all dependencies
@bbachi
bbachi / webpack.config.js
Created February 16, 2019 01:09
How to write production ready Node.js Rest API - Typescript version
var path = require('path');
module.exports = {
entry: ["./index.ts"],
output: {
filename: 'api.bundle.js',
path: path.resolve(__dirname,"dist")
},
module: {
rules: [
@bbachi
bbachi / Dockerfile
Created February 16, 2019 01:30
Containerize your Node.js Rest API and run it on Docker - Typescript version
# from base image node
FROM node:8.11-slim
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# copy oter files as well
COPY dist/api.bundle.js .
#expose the port