mirror of
https://github.com/microsoft/TypeScript-Node-Starter.git
synced 2025-11-09 16:27:25 +00:00
update from master
This commit is contained in:
1
.vscode/extensions.json
vendored
1
.vscode/extensions.json
vendored
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"recommendations": [
|
||||
"eg2.tslint",
|
||||
"ms-azuretools.vscode-cosmosdb",
|
||||
"streetsidesoftware.code-spell-checker"
|
||||
]
|
||||
}
|
||||
18
.vscode/launch.json
vendored
18
.vscode/launch.json
vendored
@@ -7,21 +7,9 @@
|
||||
{
|
||||
"type": "node",
|
||||
"request": "attach",
|
||||
"name": "Attach",
|
||||
"port": 9229
|
||||
},
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "Debug",
|
||||
"program": "${workspaceRoot}/dist/server.js",
|
||||
"smartStep": true,
|
||||
"outFiles": [
|
||||
"../dist/**/*.js"
|
||||
],
|
||||
"preLaunchTask": "npm: build",
|
||||
"name": "Attach by Process ID",
|
||||
"processId": "${command:PickProcess}",
|
||||
"protocol": "inspector"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
}
|
||||
5
.vscode/settings.json
vendored
5
.vscode/settings.json
vendored
@@ -8,5 +8,8 @@
|
||||
"typescript.referencesCodeLens.enabled": true,
|
||||
"tslint.ignoreDefinitionFiles": false,
|
||||
"tslint.autoFixOnSave": true,
|
||||
"tslint.exclude": "**/node_modules/**/*"
|
||||
"tslint.exclude": "**/node_modules/**/*",
|
||||
"cSpell.words": [
|
||||
"definitelytyped"
|
||||
]
|
||||
}
|
||||
76
README.md
76
README.md
@@ -23,6 +23,7 @@ mongod
|
||||
```
|
||||
- Build and run the project
|
||||
```
|
||||
npm run build
|
||||
npm start
|
||||
```
|
||||
Navigate to `http://localhost:3000`
|
||||
@@ -72,7 +73,8 @@ The full folder structure of this app is explained below:
|
||||
| **views** | Views define how your app renders on the client. In this case we're using pug |
|
||||
| .env.example | API keys, tokens, passwords, database URI. Clone this, but don't check it in to public repos. |
|
||||
| .travis.yml | Used to configure Travis CI build |
|
||||
| .copyStaticAssets.js | Build script that copies images, fonts, and JS libs to the dist folder |
|
||||
| .copyStaticAssets.ts | Build script that copies images, fonts, and JS libs to the dist folder |
|
||||
| jest.config.js | Used to configure Jest |
|
||||
| package.json | File that contains npm dependencies as well as [build scripts](#what-if-a-library-isnt-on-definitelytyped) |
|
||||
| tsconfig.json | Config settings for compiling server code written in TypeScript |
|
||||
| tsconfig.tests.json | Config settings for compiling tests written in TypeScript |
|
||||
@@ -144,7 +146,7 @@ Below is a list of all the scripts this template has available:
|
||||
|
||||
| Npm Script | Description |
|
||||
| ------------------------- | ------------------------------------------------------------------------------------------------- |
|
||||
| `start` | Runs full build before starting all watch tasks. Can be invoked with `npm start` |
|
||||
| `start` | Does the same as 'npm run serve'. Can be invoked with `npm start` |
|
||||
| `build` | Full build. Runs ALL build tasks (`build-sass`, `build-ts`, `tslint`, `copy-static-assets`) |
|
||||
| `serve` | Runs node on `dist/server.js` which is the apps entry point |
|
||||
| `watch` | Runs all watch tasks (TypeScript, Sass, Node). Use this if you're not touching static assets. |
|
||||
@@ -274,37 +276,19 @@ In this file, you can tell VS Code exactly what you want to do:
|
||||
```json
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "Debug",
|
||||
"program": "${workspaceRoot}/dist/server.js",
|
||||
"smartStep": true,
|
||||
"outFiles": [
|
||||
"../dist/**/*.js"
|
||||
],
|
||||
"request": "attach",
|
||||
"name": "Attach by Process ID",
|
||||
"processId": "${command:PickProcess}",
|
||||
"protocol": "inspector"
|
||||
}
|
||||
```
|
||||
This is mostly identical to the "Node.js: Launch Program" template with a couple minor changes:
|
||||
This is mostly identical to the "Node.js: Attach by Process ID" template with one minor change.
|
||||
We added `"protocol": "inspector"` which tells VS Code that we're using the latest version of Node which uses a new debug protocol.
|
||||
|
||||
| `launch.json` Options | Description |
|
||||
| ----------------------------------------------- | --------------------------------------------------------------- |
|
||||
| `"program": "${workspaceRoot}/dist/server.js",` | Modified to point to our entry point in `dist` |
|
||||
| `"smartStep": true,` | Won't step into code that doesn't have a source map |
|
||||
| `"outFiles": [...]` | Specify where output files are dropped. Use with source maps |
|
||||
| `"protocol": inspector,` | Use the new Node debug protocol because we're on the latest node|
|
||||
|
||||
With this file in place, you can hit `F5` to serve the project with the debugger already attached.
|
||||
With this file in place, you can hit `F5` to attach a debugger.
|
||||
You will probably have multiple node processes running, so you need to find the one that shows `node dist/server.js`.
|
||||
Now just set your breakpoints and go!
|
||||
|
||||
> Warning! Make sure you don't have the project already running from another command line.
|
||||
VS Code will try to launch on the same port and error out.
|
||||
Likewise be sure to stop the debugger before returning to your normal `npm start` process.
|
||||
|
||||
#### Using attach debug configuration
|
||||
VS Code debuggers also support attaching to an already running program. The `Attach` configuration has already configured, everything you need to do is change `Debug Configuration` to `Attach` and hit `F5`.
|
||||
|
||||
> Tips! Instead of running `npm start`, using `npm run debug` and `Attach Configuration` that make you don't need to stop running project to debug.
|
||||
|
||||
## Testing
|
||||
For this project, I chose [Jest](https://facebook.github.io/jest/) as our test framework.
|
||||
While Mocha is probably more common, Mocha seems to be looking for a new maintainer and setting up TypeScript testing in Jest is wicked simple.
|
||||
@@ -317,24 +301,26 @@ npm install -D jest ts-jest
|
||||
`jest` is the testing framework itself, and `ts-jest` is just a simple function to make running TypeScript tests a little easier.
|
||||
|
||||
### Configure Jest
|
||||
Jest's configuration lives in `package.json`, so let's open it up and add the following code:
|
||||
```json
|
||||
"jest": {
|
||||
"globals": {
|
||||
"__TS_CONFIG__": "tsconfig.json"
|
||||
},
|
||||
"moduleFileExtensions": [
|
||||
"ts",
|
||||
"js"
|
||||
],
|
||||
"transform": {
|
||||
"^.+\\.(ts)$": "./node_modules/ts-jest/preprocessor.js"
|
||||
},
|
||||
"testMatch": [
|
||||
"**/test/**/*.test.(ts|js)"
|
||||
],
|
||||
"testEnvironment": "node"
|
||||
},
|
||||
Jest's configuration lives in `jest.config.js`, so let's open it up and add the following code:
|
||||
```js
|
||||
module.exports = {
|
||||
globals: {
|
||||
'ts-jest': {
|
||||
tsConfigFile: 'tsconfig.json'
|
||||
}
|
||||
},
|
||||
moduleFileExtensions: [
|
||||
'ts',
|
||||
'js'
|
||||
],
|
||||
transform: {
|
||||
'^.+\\.(ts|tsx)$': './node_modules/ts-jest/preprocessor.js'
|
||||
},
|
||||
testMatch: [
|
||||
'**/test/**/*.test.(ts|js)'
|
||||
],
|
||||
testEnvironment: 'node'
|
||||
};
|
||||
```
|
||||
Basically we are telling Jest that we want it to consume all files that match the pattern `"**/test/**/*.test.(ts|js)"` (all `.test.ts`/`.test.js` files in the `test` folder), but we want to preprocess the `.ts` files first.
|
||||
This preprocess step is very flexible, but in our case, we just want to compile our TypeScript to JavaScript using our `tsconfig.json`.
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
var shell = require('shelljs');
|
||||
|
||||
shell.cp('-R', 'src/public/js/lib', 'dist/public/js/');
|
||||
shell.cp('-R', 'src/public/fonts', 'dist/public/');
|
||||
shell.cp('-R', 'src/public/images', 'dist/public/');
|
||||
5
copyStaticAssets.ts
Normal file
5
copyStaticAssets.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
const shell = require("shelljs");
|
||||
|
||||
shell.cp("-R", "src/public/js/lib", "dist/public/js/");
|
||||
shell.cp("-R", "src/public/fonts", "dist/public/");
|
||||
shell.cp("-R", "src/public/images", "dist/public/");
|
||||
18
jest.config.js
Normal file
18
jest.config.js
Normal file
@@ -0,0 +1,18 @@
|
||||
module.exports = {
|
||||
globals: {
|
||||
'ts-jest': {
|
||||
tsConfigFile: 'tsconfig.json'
|
||||
}
|
||||
},
|
||||
moduleFileExtensions: [
|
||||
'ts',
|
||||
'js'
|
||||
],
|
||||
transform: {
|
||||
'^.+\\.(ts|tsx)$': './node_modules/ts-jest/preprocessor.js'
|
||||
},
|
||||
testMatch: [
|
||||
'**/test/**/*.test.(ts|js)'
|
||||
],
|
||||
testEnvironment: 'node'
|
||||
};
|
||||
1769
package-lock.json
generated
1769
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
75
package.json
75
package.json
@@ -14,85 +14,74 @@
|
||||
"serve": "node dist/server.js",
|
||||
"watch-node": "nodemon dist/server.js",
|
||||
"watch": "concurrently -k -p \"[{name}]\" -n \"Sass,TypeScript,Node\" -c \"yellow.bold,cyan.bold,green.bold\" \"npm run watch-sass\" \"npm run watch-ts\" \"npm run watch-node\"",
|
||||
"test": "jest --forceExit",
|
||||
"test": "jest --forceExit --coverage --verbose",
|
||||
"build-ts": "tsc",
|
||||
"watch-ts": "tsc -w",
|
||||
"build-sass": "node-sass src/public/css/main.scss dist/public/css/main.css",
|
||||
"watch-sass": "node-sass -w src/public/css/main.scss dist/public/css/main.css",
|
||||
"tslint": "tslint -c tslint.json -p tsconfig.json",
|
||||
"copy-static-assets": "node copyStaticAssets.js"
|
||||
},
|
||||
"jest": {
|
||||
"globals": {
|
||||
"ts-jest": {
|
||||
"tsConfigFile": "tsconfig.json"
|
||||
}
|
||||
},
|
||||
"moduleFileExtensions": [
|
||||
"ts",
|
||||
"js"
|
||||
],
|
||||
"transform": {
|
||||
"^.+\\.(ts|tsx)$": "./node_modules/ts-jest/preprocessor.js"
|
||||
},
|
||||
"testMatch": [
|
||||
"**/test/**/*.test.(ts|js)"
|
||||
],
|
||||
"testEnvironment": "node"
|
||||
"copy-static-assets": "ts-node copyStaticAssets.ts",
|
||||
"debug": "npm run build && npm run watch-debug",
|
||||
"serve-debug": "nodemon --inspect dist/server.js",
|
||||
"watch-debug": "concurrently -k -p \"[{name}]\" -n \"Sass,TypeScript,Node\" -c \"yellow.bold,cyan.bold,green.bold\" \"npm run watch-sass\" \"npm run watch-ts\" \"npm run serve-debug\""
|
||||
},
|
||||
"dependencies": {
|
||||
"async": "^2.5.0",
|
||||
"async": "^2.6.0",
|
||||
"bcrypt-nodejs": "^0.0.3",
|
||||
"bluebird": "^3.5.1",
|
||||
"body-parser": "^1.18.2",
|
||||
"compression": "^1.7.1",
|
||||
"connect-mongo": "^1.3.2",
|
||||
"dotenv": "^2.0.0",
|
||||
"connect-mongo": "^2.0.0",
|
||||
"dotenv": "^4.0.0",
|
||||
"errorhandler": "^1.5.0",
|
||||
"express": "^4.16.2",
|
||||
"express-flash": "0.0.2",
|
||||
"express-session": "^1.15.6",
|
||||
"express-validator": "^4.2.1",
|
||||
"express-validator": "^4.3.0",
|
||||
"lodash": "^4.17.4",
|
||||
"lusca": "^1.5.2",
|
||||
"mongoose": "^4.13.7",
|
||||
"morgan": "^1.9.0",
|
||||
"nodemailer": "^2.7.2",
|
||||
"nodemailer": "^4.4.1",
|
||||
"passport": "^0.4.0",
|
||||
"passport-local": "^1.0.0",
|
||||
"pug": "^2.0.0-rc.4",
|
||||
"request": "^2.83.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/async": "^2.0.40",
|
||||
"@types/async": "^2.0.45",
|
||||
"@types/bcrypt-nodejs": "0.0.30",
|
||||
"@types/bluebird": "^3.5.18",
|
||||
"@types/body-parser": "^1.16.2",
|
||||
"@types/body-parser": "^1.16.8",
|
||||
"@types/compression": "0.0.33",
|
||||
"@types/connect-mongo": "0.0.34",
|
||||
"@types/dotenv": "^2.0.20",
|
||||
"@types/dotenv": "^4.0.2",
|
||||
"@types/errorhandler": "0.0.32",
|
||||
"@types/express": "^4.0.35",
|
||||
"@types/express-session": "^1.15.5",
|
||||
"@types/jest": "^21.1.5",
|
||||
"@types/jquery": "^3.2.15",
|
||||
"@types/lodash": "^4.14.80",
|
||||
"@types/mongodb": "^2.2.15",
|
||||
"@types/mongoose": "^4.7.9",
|
||||
"@types/express-session": "^1.15.6",
|
||||
"@types/jest": "^21.1.8",
|
||||
"@types/jquery": "^3.2.17",
|
||||
"@types/lodash": "^4.14.91",
|
||||
"@types/mongodb": "^2.2.17",
|
||||
"@types/mongoose": "^4.7.29",
|
||||
"@types/morgan": "^1.7.32",
|
||||
"@types/node": "^7.0.12",
|
||||
"@types/node": "^7.0.50",
|
||||
"@types/nodemailer": "^1.3.32",
|
||||
"@types/passport": "^0.3.3",
|
||||
"@types/request": "^2.0.7",
|
||||
"@types/supertest": "^2.0.0",
|
||||
"concurrently": "^3.4.0",
|
||||
"@types/passport-facebook": "^2.1.3",
|
||||
"@types/request": "^2.0.9",
|
||||
"@types/supertest": "^2.0.4",
|
||||
"@types/shelljs": "^0.7.7",
|
||||
"chai": "^4.1.2",
|
||||
"concurrently": "^3.5.1",
|
||||
"jest": "^21.2.1",
|
||||
"node-sass": "^4.5.2",
|
||||
"nodemon": "^1.11.0",
|
||||
"node-sass": "^4.7.2",
|
||||
"nodemon": "^1.13.0",
|
||||
"shelljs": "^0.7.7",
|
||||
"supertest": "^2.0.1",
|
||||
"ts-jest": "^21.1.4",
|
||||
"supertest": "^3.0.0",
|
||||
"ts-jest": "^21.2.4",
|
||||
"ts-node": "^4.0.2",
|
||||
"tslint": "^5.8.0",
|
||||
"typescript": "^2.4.0"
|
||||
"typescript": "^2.6.2"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,27 @@
|
||||
import * as request from "supertest";
|
||||
import * as app from "../src/app";
|
||||
|
||||
var chai = require('chai');
|
||||
var expect = chai.expect;
|
||||
|
||||
describe("GET /contact", () => {
|
||||
it("should return 200 OK", (done) => {
|
||||
request(app).get("/contact")
|
||||
.expect(200, done);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe("POST /contact", () => {
|
||||
it("should return false from assert when no message is found", (done) => {
|
||||
request(app).post("/contact")
|
||||
.field("name", "John Doe")
|
||||
.field("email", "john@me.com")
|
||||
.end(function(err, res) {
|
||||
expect(res.error).to.be.false;
|
||||
done();
|
||||
})
|
||||
.expect(302);
|
||||
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,9 @@
|
||||
import * as request from "supertest";
|
||||
import * as app from "../src/app";
|
||||
|
||||
var chai = require('chai');
|
||||
var expect = chai.expect;
|
||||
|
||||
describe("GET /login", () => {
|
||||
it("should return 200 OK", () => {
|
||||
return request(app).get("/login")
|
||||
@@ -14,3 +17,18 @@ describe("GET /signup", () => {
|
||||
.expect(200);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe("POST /login", () => {
|
||||
it("should return some defined error message with valid parameters", (done) => {
|
||||
return request(app).post("/login")
|
||||
.field("email", "john@me.com")
|
||||
.field("password", "Hunter2")
|
||||
.expect(302)
|
||||
.end(function(err, res) {
|
||||
expect(res.error).not.to.be.undefined;
|
||||
done();
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
footer
|
||||
.container.text-center
|
||||
p.pull-left © 2016 Company, Inc. All Rights Reserved
|
||||
p.pull-left © 2018 Company, Inc. All Rights Reserved
|
||||
iframe.pull-right(src="https://ghbtns.com/github-btn.html?user=Microsoft&repo=TypeScript-Node-Starter&type=star&count=true" frameborder="0" scrolling="0" width="90px" height="20px" style="margin-top:15px")
|
||||
|
||||
Reference in New Issue
Block a user