Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
saa committed Jan 19, 2018
0 parents commit ee945f5
Show file tree
Hide file tree
Showing 19 changed files with 4,383 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Vladimir Saakyan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
### Modern Express | Easy starter project for writing modern Express MongoDb Applications using TypeScript and Webpack

This project is used to quickly get started with Webpack using Express, MongoDb, Webpack and TypeScript. The intention of this project is to learn to use Webpack with out getting bogged down in the details of configuring Webpack. Below is a list of the outcomes of this project.

1. Automates the task of setting up Webpack
2. Cleanly installs and setups Webpack with out polluting the global system scope
3. Creates basic Express project to write TypeScript
4. Cleanly builds TypeScript code into a build directory
5. Stores and Saves Typings for VSCode
6. Uses `ejs` as the templating engine

**Setup**
---
**[Clone this Repository](https://github.com/lossless1/nodets-webpack-starter/archive/master.zip)**

```
yarn install
```

**Run Builds**
---
```
yarn run build
```

**Run Application**
---
```
yarn start
mongod
```
90 changes: 90 additions & 0 deletions bin/www.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* Module dependencies.
*/
import * as http from 'http';
import * as debug from 'debug';
import * as config from '../src/libs/config';

import { Server } from '../src/server';
import { nconf } from '../src/libs/config';

class Www {

constructor() {
}

public bootstrap() {

// binding to console
let log = debug('modern-express:server');
log.log = console.log.bind(console);

/**
* Get port from environment and store in Express.
*/
let PORT = process.env.PORT || this.getPort(nconf.get('port'));


let base = new Server();

base.app.set('port', PORT);

/**
* Create HTTP server.
*/
const server = http.createServer(base.app);

/**
* Listen on provided port, on all network interfaces.
*/
server.listen(PORT);

server.on('error', (error: any) => {
/**
* Event listener for HTTP server "error" event.
*/
if (error.syscall !== 'listen') {
throw error;
}
const bind = typeof PORT === 'string' ? `Pipe ${PORT}` : `Port ${PORT}`;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
});

server.on('listening', () => {
/**
* Event listener for HTTP server "listening" event.
*/
const addr = server.address();
const bind = (typeof addr === 'string' ? `pipe ${addr}` : `port ${addr.port}`);
log(`Listening on ${bind}`);
});
}
public getPort(val) {
/**
* Normalize a port into a number, string, or false.
*/
const port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
}
new Www().bootstrap();
6 changes: 6 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"port": 1337,
"mongoose": {
"uri": "mongodb://localhost/test1"
}
}
43 changes: 43 additions & 0 deletions config/webpack.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { path } from 'app-root-path';

export default {
entry: `${path}/bin/www.ts`,
target: 'node',
externals: [
/^[a-z\-0-9]+$/ // Ignore node_modules folder
],
output: {
filename: 'compiled', // output file
path: `${path}/build`,
libraryTarget: "commonjs"
},
resolve: {
// Add in `.ts` and `.tsx` as a resolvable extension.
extensions: ['.webpack.js', '.web.js', '.ts', '.tsx', '.js'],
modules: [
`${path}/node_modules`,
'node_modules'
]
},
resolveLoader: {
//root: [`${root}/node_modules`],


},
module: {
rules: [{
// all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
test: /\.tsx?$/,
use: [
{
loader: 'ts-loader',
}
]
}]
},
plugins: [
// new writeFilePlugin({
// test: /foo/
// })
]
};
62 changes: 62 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"name": "webpack-express-typescript",
"version": "0.0.1",
"description": "Easy starter project for writing modern Express Applications using TypeScript, and Webpack",
"private": true,
"directories": {
"test": "tests"
},
"scripts": {
"server:dev:nodemon": "NODE_ENV=development nodemon --watch '**/*.ts' --ignore '**/*.spec.ts' --exec 'ts-node' bin/www.ts",
"build": "ts-node node_modules/webpack/bin/webpack.js --config config/webpack.config.ts",
"compiled": "ts-node build/compiled",
"clean": "rimraf build",
"start": "yarn run server:dev:nodemon"
},
"main": "",
"keywords": [
"Easy",
"TypeScript",
"Starter",
"Template"
],
"author": "Vladimir Saakyan",
"license": "MIT",
"repository": {
"type": "git",
"url": ""
},
"dependencies": {
"app-root-path": "^2.0.1",
"body-parser": "^1.17.1",
"cookie-parser": "^1.4.3",
"core-js": "^2.5.3",
"ejs": "^2.5.1",
"errorhandler": "^1.5.0",
"express": "^4.15.2",
"method-override": "^2.3.10",
"mongodb": "^3.0.1",
"mongoose": "^5.0.0",
"morgan": "^1.7.0",
"nconf": "^0.10.0",
"node": "^9.3.0",
"winston": "^3.0.0-rc1"
},
"devDependencies": {
"@types/app-root-path": "^1.2.4",
"@types/body-parser": "^1.16.8",
"@types/cookie-parser": "^1.4.1",
"@types/debug": "^0.0.30",
"@types/mime": "^2.0.0",
"@types/mongodb": "^3.0.2",
"@types/mongoose": "^4.7.32",
"@types/nconf": "^0.0.36",
"@types/winston": "^2.3.7",
"nodemon": "^1.14.10",
"ts-loader": "^2.0.2",
"ts-node": "^4.1.0",
"typescript": "^2.2.1",
"typings": "^1.3.3",
"webpack": "^2.2.1"
}
}
6 changes: 6 additions & 0 deletions src/libs/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as nconf from 'nconf';

nconf.argv().env().file({
file: './config.json'
})
export { nconf };
43 changes: 43 additions & 0 deletions src/libs/mongoose.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Article } from './../models/Article';
import * as mongoose from 'mongoose';
import log from './winston.error';
import { nconf } from './config';

mongoose.connect(nconf.get('mongoose:uri'));

let db = mongoose.connection;

db.on('error', (err) => {
log.error('connection error:', err.message);
})

db.once('open', () => {
log.info('Connection successfull');
})

let Schema = mongoose.Schema;

let Images = new Schema({
kind: {
type: String,
enum: ['thumbnail', 'detail'],
required: true
},
url: { type: String, required: true }
})

let ArticleSchema = new Schema({
title: { type: String, required: true },
author: { type: String, required: true },
description: { type: String, required: true },
images: [Images],
modified: { type: Date, default: Date.now }
})

ArticleSchema.path('title').validate((v) => {
return v.length > 5 && v.length < 70;
})

let ArticleModel = mongoose.model('Article', ArticleSchema);

export { ArticleModel };
13 changes: 13 additions & 0 deletions src/libs/winston.error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { transports, createLogger, format } from 'winston';
import * as path from 'path';

export default createLogger({
transports: [
new transports.Console({
colorize: true,
level: 'debug',
label: path
}),
new transports.File({ filename: 'src/logs/combined.log' })
]
});
Loading

0 comments on commit ee945f5

Please sign in to comment.