From 2a0401be7f8e280044f5e83ffc52babcd5f508ac Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Mon, 25 Mar 2019 11:52:24 +0100 Subject: [PATCH] testing tweaks --- README.md | 2 +- package.json | 4 +-- scripts/test.sh | 0 server/src/config/config.ts | 4 ++- server/src/index.ts | 43 +++++++++++++++-------------- server/src/routes/UrlCheckRouter.ts | 16 +++++++---- 6 files changed, 39 insertions(+), 30 deletions(-) mode change 100644 => 100755 scripts/test.sh diff --git a/README.md b/README.md index 1d0e8f9..387191f 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ npm start ``` This will run client and server in development mode.
-Open [http://localhost:3000](http://localhost:3000) to view the client in the browser. The server is +Open [http://localhost:3000](http://localhost:3000) to view the client in the browser. The page will reload if you make edits to files in either `./client` or `./server`. diff --git a/package.json b/package.json index 1350d36..354b34a 100644 --- a/package.json +++ b/package.json @@ -8,9 +8,9 @@ "build": "./scripts/build.sh", "test": "npm run lint && scripts/test.sh", "format:js": "prettier --parser typescript --write '**/*.{js,jsx,ts,tsx}'", - "format:css": "prettier-stylelint --write --quiet '**/*.{css,scss}'", + "format:css": "prettier-stylelint --ignore-path .gitignore --write --quiet '**/*.{css,scss}'", "format": "npm run format:js && npm run format:css", - "lint:css": "stylelint './**/*.{css,scss}'", + "lint:css": "stylelint --ignore-path .gitignore './**/*.{css,scss}'", "lint:js": "eslint --ignore-path .gitignore --ignore-path .prettierignore --ext .ts,.tsx .", "lint:fix": "eslint --fix --ignore-path .gitignore --ignore-path .prettierignore --ext .ts,.tsx .", "lint": "npm run lint:js && npm run lint:css" diff --git a/scripts/test.sh b/scripts/test.sh old mode 100644 new mode 100755 diff --git a/server/src/config/config.ts b/server/src/config/config.ts index 2bb3e30..ef9bbe0 100644 --- a/server/src/config/config.ts +++ b/server/src/config/config.ts @@ -1,5 +1,7 @@ -module.exports = { +const config = { app: { port: 4000 } } + +export default config diff --git a/server/src/index.ts b/server/src/index.ts index 943e423..71a0f7a 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -8,12 +8,31 @@ import bodyParser from 'body-parser' import UrlCheckRouter from './routes/UrlCheckRouter' // config -const config = require('./config/config') +import config from './config/config' // debug const log = debug('server:index') const app = express() + +function onListening(): void { + log('Server thread started') +} + +function onError(error: NodeJS.ErrnoException): void { + if (error.syscall !== 'listen') throw error + switch (error.code) { + case 'EACCES': + log('Required elevated privileges') + process.exit(1) + case 'EADDRINUSE': + log('Port is already in use') + process.exit(1) + default: + throw error + } +} + app.use((req, res, next) => { res.header('Access-Control-Allow-Origin', '*') res.header( @@ -32,33 +51,15 @@ app.use(bodyParser.urlencoded({ extended: false })) app.use(compression()) // routes app.use('/api/v1/urlcheck', UrlCheckRouter) + /// catch 404 app.use((req, res, next) => { res.status(404).send() }) + // listen const server = app.listen(config.app.port) server.on('listening', onListening) server.on('error', onError) -function onListening(): void { - log('Server thread started') -} - -function onError(error: NodeJS.ErrnoException): void { - if (error.syscall !== 'listen') throw error - switch (error.code) { - case 'EACCES': - log('Required elevated privileges') - process.exit(1) - break - case 'EADDRINUSE': - log('Port is already in use') - process.exit(1) - break - default: - throw error - } -} - export default server diff --git a/server/src/routes/UrlCheckRouter.ts b/server/src/routes/UrlCheckRouter.ts index 2ff960e..360db7c 100644 --- a/server/src/routes/UrlCheckRouter.ts +++ b/server/src/routes/UrlCheckRouter.ts @@ -2,12 +2,12 @@ import { Router, Request, Response, NextFunction } from 'express' import request from 'request' export class UrlCheckRouter { - router: Router + public router: Router /** * Initialize the UrlCheckRouter */ - constructor() { + public constructor() { this.router = Router() } @@ -25,22 +25,28 @@ export class UrlCheckRouter { if (response.statusCode.toString().startsWith('2')) { const result: any = {} result.found = true + if (response.headers['content-length']) { result.contentLength = response.headers['content-length'] } + if (response.headers['content-type']) { const typeAndCharset = response.headers[ 'content-type' ].split(';') - result.contentType = typeAndCharset[0] + + result.contentType = typeAndCharset[0] // eslint-disable-line prefer-destructuring + if (typeAndCharset[1]) { + /* eslint-disable prefer-destructuring */ result.contentCharset = typeAndCharset[1].split( '=' )[1] + /* eslint-enable prefer-destructuring */ } } - return res.send({ status: 'success', result: result }) + return res.send({ status: 'success', result }) } return res.send({ status: 'error', message: error }) } @@ -51,7 +57,7 @@ export class UrlCheckRouter { * Take each handler, and attach to one of the Express.Router's * endpoints. */ - init() { + public init() { this.router.post('/', this.checkUrl) } }