{"id":13508990,"url":"https://github.com/babel/example-node-server","last_synced_at":"2025-04-07T01:06:17.166Z","repository":{"id":44524896,"uuid":"41663332","full_name":"babel/example-node-server","owner":"babel","description":"Example Node Server w/ Babel","archived":false,"fork":false,"pushed_at":"2023-01-03T15:18:10.000Z","size":386,"stargazers_count":2841,"open_issues_count":36,"forks_count":383,"subscribers_count":56,"default_branch":"master","last_synced_at":"2024-10-29T15:14:20.230Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/babel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["babel"],"open_collective":"babel","custom":"https://gitcoin.co/grants/2906/babel-compiler-for-next-generation-javascript"}},"created_at":"2015-08-31T08:02:08.000Z","updated_at":"2024-10-26T06:55:43.000Z","dependencies_parsed_at":"2023-02-01T07:00:48.847Z","dependency_job_id":null,"html_url":"https://github.com/babel/example-node-server","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/babel%2Fexample-node-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/babel%2Fexample-node-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/babel%2Fexample-node-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/babel%2Fexample-node-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/babel","download_url":"https://codeload.github.com/babel/example-node-server/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246175533,"owners_count":20735579,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T02:01:01.466Z","updated_at":"2025-04-07T01:06:17.133Z","avatar_url":"https://github.com/babel.png","language":"JavaScript","readme":"# Example Node Server w/ Babel\n\n### Getting Started\n\nFirst we'll install `@babel/cli`, `@babel/core` and `@babel/preset-env`.\n\n```shell\n$ npm install --save-dev @babel/cli @babel/core @babel/preset-env\n```\n\nThen we'll create a `.babelrc` file for configuring babel.\n\n```shell\n$ touch .babelrc\n```\n\nThis will host any options we might want to configure `babel` with.\n\n```json\n{\n  \"presets\": [\"@babel/preset-env\"]\n}\n```\n\nThen create our server in `index.js`.\n\n```shell\n$ touch index.js\n```\n```js\nimport http from 'http';\n\nconst server = http.createServer((req, res) =\u003e {\n  res.writeHead(200, {'Content-Type': 'text/plain'});\n  res.end('Hello World\\n');\n}).listen(1337, '127.0.0.1');\n\nconsole.log('Server running at http://127.0.0.1:1337/');\n\nexport default server;\n```\n\nWith recent changes to babel, you will need to transpile your ES6 before node can run it.\n\nSo, we'll add our first script, `build`, in `package.json`.\n\n```diff\n  \"scripts\": {\n+   \"build\": \"babel index.js -d dist\"\n  }\n```\n\nThen we'll add our `start` script in `package.json`.\n\n\n```diff\n  \"scripts\": {\n   \"build\": \"babel index.js -d dist\",\n+   \"start\": \"npm run build \u0026\u0026 node dist/index.js\"\n  }\n```\n\nNow let's start our server.\n\n```shell\n$ npm start\n```\n\nYou should now be able to visit `http://127.0.0.1:1337` and see `Hello World`.\n\n### Watching file changes with `nodemon`\n\nWe can improve our `npm start` script with `nodemon`.\n\n```shell\n$ npm install --save-dev nodemon\n```\n\nThen we can update our `npm start` script.\n\n```diff\n  \"scripts\": {\n    \"build\": \"babel index.js -d dist\",\n-   \"start\": \"npm run build \u0026\u0026 node dist/index.js\"\n+   \"start\": \"npm run build \u0026\u0026 nodemon dist/index.js\"\n  }\n```\n\nThen we'll restart our server.\n\n```shell\n$ npm start\n```\n\nYou should now be able to make changes to `index.js` and our server should be\nrestarted automatically by `nodemon`.\n\nGo ahead and replace `Hello World` with `Hello {{YOUR_NAME_HERE}}` while our\nserver is running.\n\nIf you visit `http://127.0.0.1:1337` you should see our server greeting you.\n\n### Getting ready for production use\n\nFirst let's move our server `index.js` file to `lib/index.js`.\n\n```shell\n$ mkdir lib\n$ mv index.js lib/index.js\n```\n\nAnd update our `npm start` script to reflect the location change.\n\n```diff\n  \"scripts\": {\n-   \"build\": \"babel index.js -d dist\",\n+   \"build\": \"babel lib -d dist\",\n    \"start\": \"npm run build \u0026\u0026 nodemon dist/index.js\"\n  }\n```\n\nNext let's add a new task: `npm run serve`.\n\n```diff\n  \"scripts\": {\n    \"build\": \"babel lib -d dist\",\n    \"start\": \"npm run build \u0026\u0026 nodemon dist/index.js\",\n+   \"serve\": \"node dist/index.js\"\n  }\n```\n\nNow we can use `npm run build` for precompiling our assets, and `npm run serve`\nfor starting our server in production.\n\n```shell\n$ npm run build\n$ npm run serve\n```\n\nThis means we can quickly restart our server without waiting for `babel` to\nrecompile our files.\n\nOh, let's not forget to add `dist` to our `.gitignore` file:\n\n```shell\n$ touch .gitignore\n```\n\n```\ndist\n```\n\nThis will make sure we don't accidentally commit our built files to git.\n\n### Testing the server\n\nFinally let's make sure our server is well tested.\n\nLet's install `mocha`.\n\n```shell\n$ npm install --save-dev mocha\n```\n\nAnd create our test in `test/index.js`.\n\n```shell\n$ mkdir test\n$ touch test/index.js\n```\n\n```js\nimport http from 'http';\nimport assert from 'assert';\n\nimport server from '../lib/index.js';\n\ndescribe('Example Node Server', () =\u003e {\n  it('should return 200', done =\u003e {\n    http.get('http://127.0.0.1:1337', res =\u003e {\n      assert.equal(200, res.statusCode);\n      server.close();\n      done();\n    });\n  });\n});\n```\n\nNext, install `@babel/register` for the require hook.\n\n```shell\n$ npm install --save-dev @babel/register\n```\n\nThen we can add an `npm test` script.\n\n```diff\n  \"scripts\": {\n    \"start\": \"nodemon lib/index.js --exec babel-node\",\n    \"build\": \"babel lib -d dist\",\n    \"serve\": \"node dist/index.js\",\n+   \"test\": \"mocha --require @babel/register\"\n  }\n```\n\nNow let's run our tests.\n\n```shell\n$ npm test\n```\n\nYou should see the following:\n\n```shell\nServer running at http://127.0.0.1:1337/\n\n  Example Node Server\n    ✓ should return 200\n\n  1 passing (43ms)\n```\n\nThat's it!\n","funding_links":["https://github.com/sponsors/babel","https://opencollective.com/babel","https://gitcoin.co/grants/2906/babel-compiler-for-next-generation-javascript"],"categories":["JavaScript","\u003ca name=\"JavaScript\"\u003e\u003c/a\u003eJavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbabel%2Fexample-node-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbabel%2Fexample-node-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbabel%2Fexample-node-server/lists"}