Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
feat: improve interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Aug 11, 2019
1 parent 5f98239 commit 6cefa7a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 118 deletions.
153 changes: 55 additions & 98 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
[![Dependency status](https://img.shields.io/david/Kikobeats/svr.svg?style=flat-square)](https://david-dm.org/Kikobeats/svr)
[![Dev Dependencies Status](https://img.shields.io/david/dev/Kikobeats/svr.svg?style=flat-square)](https://david-dm.org/Kikobeats/svr#info=devDependencies)
[![NPM Status](https://img.shields.io/npm/dm/svr.svg?style=flat-square)](https://www.npmjs.org/package/svr)
[![Donate](https://img.shields.io/badge/donate-paypal-blue.svg?style=flat-square)](https://paypal.me/Kikobeats)

> HTTP development server done right
Expand All @@ -28,82 +27,64 @@ It's similar [micro-dev](https://github.com/zeit/micro-dev), but out of the box

## Installation

You can install it as global

```bash
$ npm install svr --global
```

Or use it as part of your development workflow as a `devDependency`:

```bash
$ npm install svr --save-dev
$ npm install svr --save
```

## Usage

### Getting Started
### Defining entry point

After installation, just call it:
Create a file and export a function that accepts the standard `http.IncomingMessage` and `http.ServerResponse` objects, that means, the exported function should be receive `req, res`:

```bash
$ svr
```
```js
const express = require('express')
const { Router } = express
const router = Router()

We recommend add **svr** as npm script:
// define middlewares
router.use(require('helmet')())
router.use(require('compression')())
router.use(require('cors')())

```json
{
"scripts": {
"dev": "srv"
}
}
// define routes
router.get('/', (req, res) => res.status(204).send())
router.get('/robots.txt', (req, res) => res.status(204).send())
router.get('/favicon.txt', (req, res) => res.status(204).send())

// expose router
module.exports = router
```

Now, running `npm run dev` it will be start your HRM development server:
After that, just call **svr**:

```bash
$ npm start

┌───────────────────────────────────────────────────┐
│ │
│ my-express-api is running!
│ │
│ • Local: http://localhost:3000 │
│ • On Your Network: http://192.168.1.106:3000 │
│ │
└───────────────────────────────────────────────────┘
$ svr
```

**svr** is assuming you have a `main` file declared in your `package.json` in the project directory.

Otherwise, you can provide the file path as first argument:
Also, you can provide it as first argument:

```bash
$ svr src/server/routes.js
$ svr index.js
```

The only requirement is define the main file of your server as exported function that follow this interface:
We recommend setup **svr** as npm script:

```js
module.exports = (app, express) => {
/* your awesome code here */
```json
{
"scripts": {
"dev": "srv"
}
}
```

If your project directory is different from the current directory you can specify it as well using `-d` or `--cwd` flag:
Now, running `npm run dev` it will be start your HRM development server:

```bash
$ svr src/server/routes.js --cwd=~/Projects/my-express-api
```

Type `svr --help` to get all the information.

### Watching for changes

After start, whatever file modification in the project directory will be listened by **svr** automagically:
$ npm start

```bash
┌───────────────────────────────────────────────────┐
│ │
│ my-express-api is running!
Expand All @@ -112,11 +93,13 @@ After start, whatever file modification in the project directory will be listene
│ • On Your Network: http://192.168.1.106:3000 │
│ │
└───────────────────────────────────────────────────┘

ℹ 18:32:42 modified index.js
```

If you need to reload the server on demand, just type `rs`:
You can type `svr --help` to see all the options.

### Hot reloading

whatever file modification in the project directory will be listened by **svr** automagically:

```bash
┌───────────────────────────────────────────────────┐
Expand All @@ -129,22 +112,8 @@ If you need to reload the server on demand, just type `rs`:
└───────────────────────────────────────────────────┘

ℹ 18:32:42 modified index.js
rs
ℹ 18:34:07 restart index.js
```

**svr** only will be listen files in the current directory by default.

You can use `-w` or `--watch` to add more file path to be listened

```bash
$ svr src/server/routes.js
```

Type `svr --help` to get all the information.

### Ignoring files

**svr** takes into consideration ignore non relevant files.

By default, it will be to ignore:
Expand All @@ -153,53 +122,41 @@ By default, it will be to ignore:
- `.gitignore` declarations.
- `ignored` field in your `package.json`.

You can declare:

- Relative or absolute paths.
- Glob patterns.

If you need to add a specific file to ignore, use `i` or `--ignore` flag:

```bash
$ svr -i .cache -i public
```

## Tips
Also, you can use `-w` or `--watch` to add more file path to be listened. You can declare:

### Main file
- Relative or absolute paths.
- Glob patterns.

This could be a good start point for a HTTP server:
## Manual reloading

```js
const isProduction = process.env.NODE_ENV === 'production'

module.exports = (app, express) => {
/* here you can do whatever you want */
app
.use(require('helmet')())
.use(require('compression')())
.use(require('cors')())
.use(require('jsendp')())
.use(require('express-status-monitor')())
.use(bodyParser.urlencoded({ extended: true }))
.use(bodyParser.json())
.use(require('morgan')(isProduction ? 'combined' : 'dev'))
.use(express.static('static'))
.disable('x-powered-by')

app.get('/', async function (req, res) {
return res.send('hello world')
})

return app
}
If you need to reload the server on demand, just type `rs`:

```bash
┌───────────────────────────────────────────────────┐
│ │
│ my-express-api is running!
│ │
│ • Local: http://localhost:3000 │
│ • On Your Network: http://192.168.1.106:3000 │
│ │
└───────────────────────────────────────────────────┘

ℹ 18:32:42 modified index.js
rs
ℹ 18:34:07 restart index.js
```

### Production Server

**svr** is oriented just for development scenarios.

Under production, simply create the boostraping server that you need.
Under production, simply create the server you need based on your necessities.

For example, let's create a `bin/server` as production server:

Expand Down
14 changes: 7 additions & 7 deletions bin/serve/listen.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
'use strict'

const importCwd = require('import-cwd')
const express = require('express')

const { error: logError } = require('../cli/log')
const listenMessage = require('./listen-message')

const createServer = filepath =>
express()
.use(require(filepath))
.disable('x-powered-by')

module.exports = async ({ userPort, inUse, pkg, port, host, restarting, filepath }) => {
let server

try {
const module = require(filepath)
const express = importCwd('express')
const app = express()
await module(app, express)

server = app.listen(port, host, () => {
server = createServer(filepath).listen(port, host, () => {
if (!restarting) {
const message = listenMessage({
appName: pkg.name || 'svr',
Expand Down
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
"url": "https://github.com/Kikobeats"
},
"contributors": [
"Kiko Beats <josefrancisco.verdu@gmail.com>"
{
"name": "Kiko Beats",
"email": "josefrancisco.verdu@gmail.com"
}
],
"repository": {
"type": "git",
Expand Down Expand Up @@ -46,10 +49,10 @@
"clean-stack": "~2.1.0",
"clear-module": "~3.2.0",
"debounce": "~1.2.0",
"express": "~4.17.1",
"get-port": "~5.0.0",
"ignore-by-default": "~1.0.1",
"ignored": "~2.0.4",
"import-cwd": "~3.0.0",
"indent-string": "~4.0.0",
"ip": "~1.1.5",
"load-json-file": "~6.1.0",
Expand Down Expand Up @@ -102,7 +105,6 @@
"dev:docs:server": "browser-sync start --server --files \"index.html, README.md, static/**/*.(css|js)\"",
"lint": "standard-markdown README.md && standard",
"postrelease": "npm run release:tags && npm run release:github && ci-publish",
"precommit": "lint-staged",
"prerelease": "npm run update:check",
"pretty": "prettier-standard {core,test,bin}/**/*.js",
"release": "git-authors-cli && git add package.json && standard-version -a",
Expand All @@ -125,16 +127,14 @@
}
},
"lint-staged": {
"linters": {
"package.json": [
"finepack",
"git add"
],
"*.js": [
"prettier-standard",
"git add"
]
}
"package.json": [
"finepack",
"git add"
],
"*.js": [
"prettier-standard",
"git add"
]
},
"standard": {
"env": [
Expand Down

0 comments on commit 6cefa7a

Please sign in to comment.