Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Mar 9, 2025. It is now read-only.

Example Node Server w/ Babel

License

NotificationsYou must be signed in to change notification settings

babel/example-node-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Getting Started

First we'll install@babel/cli,@babel/core and@babel/preset-env.

$ npm install --save-dev @babel/cli @babel/core @babel/preset-env

Then we'll create a.babelrc file for configuring babel.

$ touch .babelrc

This will host any options we might want to configurebabel with.

{"presets": ["@babel/preset-env"]}

Then create our server inindex.js.

$ touch index.js
importhttpfrom'http';constserver=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/');exportdefaultserver;

With recent changes to babel, you will need to transpile your ES6 before node can run it.

So, we'll add our first script,build, inpackage.json.

  "scripts": {+   "build": "babel index.js -d dist"  }

Then we'll add ourstart script inpackage.json.

  "scripts": {   "build": "babel index.js -d dist",+   "start": "npm run build && node dist/index.js"  }

Now let's start our server.

$ npm start

You should now be able to visithttp://127.0.0.1:1337 and seeHello World.

Watching file changes withnodemon

We can improve ournpm start script withnodemon.

$ npm install --save-dev nodemon

Then we can update ournpm start script.

  "scripts": {    "build": "babel index.js -d dist",-   "start": "npm run build && node dist/index.js"+   "start": "npm run build && nodemon dist/index.js"  }

Then we'll restart our server.

$ npm start

You should now be able to make changes toindex.js and our server should berestarted automatically bynodemon.

Go ahead and replaceHello World withHello {{YOUR_NAME_HERE}} while ourserver is running.

If you visithttp://127.0.0.1:1337 you should see our server greeting you.

Getting ready for production use

First let's move our serverindex.js file tolib/index.js.

$ mkdir lib$ mv index.js lib/index.js

And update ournpm start script to reflect the location change.

  "scripts": {-   "build": "babel index.js -d dist",+   "build": "babel lib -d dist",    "start": "npm run build && nodemon dist/index.js"  }

Next let's add a new task:npm run serve.

  "scripts": {    "build": "babel lib -d dist",    "start": "npm run build && nodemon dist/index.js",+   "serve": "node dist/index.js"  }

Now we can usenpm run build for precompiling our assets, andnpm run servefor starting our server in production.

$ npm run build$ npm run serve

This means we can quickly restart our server without waiting forbabel torecompile our files.

Oh, let's not forget to adddist 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 installmocha.

$ npm install --save-dev mocha

And create our test intest/index.js.

$ mkdirtest$ touch test/index.js
importhttpfrom'http';importassertfrom'assert';importserverfrom'../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);server.close();done();});});});

Next, install@babel/register for the require hook.

$ npm install --save-dev @babel/register

Then we can add annpm test script.

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

Now let's run our tests.

$ npmtest

You should see the following:

Server running at http://127.0.0.1:1337/  Example Node Server    ✓ shouldreturn 200  1 passing (43ms)

That's it!

About

Example Node Server w/ Babel

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

  •  

Packages

No packages published

Contributors8


[8]ページ先頭

©2009-2025 Movatter.jp