https://github.com/kth/kth-node-server
Module for initializing an Express server in a Node.js application.
https://github.com/kth/kth-node-server
Last synced: 7 days ago
JSON representation
Module for initializing an Express server in a Node.js application.
- Host: GitHub
- URL: https://github.com/kth/kth-node-server
- Owner: KTH
- License: mit
- Created: 2016-08-31T15:36:00.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2024-04-26T14:07:07.000Z (about 2 years ago)
- Last Synced: 2024-04-26T15:27:46.006Z (about 2 years ago)
- Language: JavaScript
- Size: 198 KB
- Stars: 0
- Watchers: 19
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# @kth/server
A wrapper around express.
In it's most basic usage, it just exposes an express app, but it has support for starting with ssl.
## Simple usage, starts a server on http
Start returns a promise that resolves when startup is complete.
```JavaScript
const app = require('@kth/server')
app.start()
.then((res) => {
// Do something...
})
```
The import returns an Express.js instance, so you can add middleware and functions just as in a normal express app:
```JavaScript
app.get('/', function (req, res) {
res.send('Hello world!')
})
```
To stop the instance you call app.close() which returns a promise that resolves when done.
```JavaScript
app.close()
.then(() => {
// Do something...
})
```
## Start server with options
### Secure HTTPS
```JavaScript
const app = require('@kth/server')
const optionsForSsl = {
useSsl: true,
pfx: '/path/to/file.pfx',
passphrase: '/path/to/file.txt',
port: 3000, // Optional, defaults to 3000
logger: console // Optional, defaults to console
}
app.start(optionsForSsl)
```
### Plain HTTP
```JavaScript
const app = require('@kth/server')
const optionsForSsl = {
port: 3000, // Optional, defaults to 3000
logger: console // Optional, defaults to console
}
app.start(optionsForSsl)
```
## Creating a self signed cert for testing
```
$ openssl genrsa 2048 > test/certs/private.pem
$ openssl req -x509 -days 1000 -new -key test/certs/private.pem -out test/certs/public.pem -subj "/C=SE/ST=SWEDEN/L=Provo/O=kth-node-server/CN=www.test.com"
$ openssl pkcs12 -export -in test/certs/public.pem -inkey test/certs/private.pem -passout pass:test -out test/certs/withpassphrase.pfx
$ echo 'test' >> test/certs/passphrase.txt
$ rm test/certs/private.pem test/certs/public.pem
```
## TODO
TODO: write test for signing requests with cert