Skip to content

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiebuilds committed Aug 31, 2015
0 parents commit d9388d3
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"stage": 2
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modules
5 changes: 5 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Copyright (c) 2015, James Kyle <me@thejameskyle.com>

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
206 changes: 206 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
# Example Node Server w/ Babel

### Getting Started

First we'll install `babel`.

```shell
$ npm install --save-dev babel
```

Then create our server in `index.js`.

```shell
$ touch index.js
```
```js
import http from 'http';

http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');

console.log('Server running at http://127.0.0.1:1337/');
```

Then we'll add our first `npm start` script in `package.json`.

```diff
"scripts": {
+ "start": "babel-node index.js"
}
```

Now let's start our server.

```shell
$ npm start
```

You should now be able to visit `http://127.0.0.1:1337` and see `Hello World`.

### Watching file changes with `nodemon`

We can improve our `npm start` script with `nodemon`.

```shell
$ npm install --save-dev nodemon
```

Then we can update our `npm start` script.

```diff
"scripts": {
- "start": "babel-node index.js"
+ "start": "nodemon index.js --exec babel-node"
}
```

Then we'll restart our server.

```shell
$ npm start
```

You should now be able to make changes to `index.js` and our server should be
restarted automatically by `nodemon`.

Go ahead and replace `Hello World` with `Hello {{YOUR_NAME_HERE}}` while our
server is running.

If you visit `http://127.0.0.1:1337` you should see our server greeting you.

### Getting ready for production use

So we've cheated a little bit by using `babel-node`. While this is great for
getting something going. It's not a good idea to use it in production.

We should be precompiling your files, so let's do that now.

First let's move our server `index.js` file to `lib/index.js`.

```shell
$ mv index.js lib/index.js
```

And update our `npm start` script to reflect the location change.

```diff
"scripts": {
- "start": "nodemon index.js --exec babel-node"
+ "start": "nodemon lib/index.js --exec babel-node"
}
```

Next let's add two new tasks `npm run build` and `npm run serve`.

```diff
"scripts": {
"start": "nodemon lib/index.js --exec babel-node",
+ "build": "babel lib -d dist",
+ "serve": "node dist/index.js"
}
```

Now we can use `npm run build` for precompiling our assets, and `npm run serve`
for starting our server in production.

```shell
$ npm run build
$ npm run serve
```

This means we can quickly restart our server without waiting for `babel` to
recompile our files.

Oh let's not forget to add `dist` to our `.gitignore` file.

```
$ touch .gitignore
```
dist
```
This will make sure we don't accidentally commit our built files to git.
### Testing the server
Finally let's make sure our server is well tested.
Let's install `mocha`.
```shell
$ npm install --save-dev mocha
```

And create our test in `test/index.js`.

```shell
$ mkdir test
$ touch test/index.js
```

```js
import http from 'http';
import assert from 'assert';

import 'lib/index.js';

describe('Example Node Server', () => {
it('should return 200', done => {
http.get('http://127.0.0.1:1337', res => {
assert.equal(200, res.statusCode);
done();
});
});
});
```

Then we can add an `npm test` script.

```diff
"scripts": {
"start": "nodemon lib/index.js --exec babel-node",
"build": "babel lib -d dist",
"serve": "node dist/index.js",
+ "test": "mocha --compilers js:babel/register"
}
```

Now let's run our tests.

```shell
$ npm test
```

You should see the following:

```shell
Server running at http://127.0.0.1:1337/

Example Node Server
✓ should return 200

1 passing (43ms)
```

### Saving Babel options to `.babelrc`

Let's create a `.babelrc` file.

```shell
$ touch .babelrc
```

This will host any options we might want to configure `babel` with.

```json
{
"stage": 2
}
```

That's it!

8 changes: 8 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import http from 'http';

http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');

console.log('Server running at http://127.0.0.1:1337/');
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "example-node-server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon lib/index.js --exec babel-node",
"build": "babel lib -d dist",
"serve": "node dist/index.js",
"test": "mocha --compilers js:babel/register"
},
"author": "James Kyle <me@thejameskyle.com>",
"license": "ISC",
"devDependencies": {
"babel": "^5.8.23",
"mocha": "^2.3.0",
"nodemon": "^1.4.1"
}
}
13 changes: 13 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import http from 'http';
import assert from 'assert';

import '../lib/index.js';

describe('Example Node Server', () => {
it('should return 200', done => {
http.get('http://127.0.0.1:1337', res => {
assert.equal(200, res.statusCode);
done();
});
});
});

0 comments on commit d9388d3

Please sign in to comment.