Giter VIP home page Giter VIP logo

example-node-server's People

Contributors

also avatar dandv avatar danez avatar eventualbuddha avatar jamiebuilds avatar kulakowka avatar waldyrious avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

example-node-server's Issues

Reason for precompiling

Hello,

It says the following in README.md:

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.

I'm curious, why exactly is it not a good idea? Isn't the code getting executed the same? And as servers are generally longer-running then clients, the code doesn't get recompiled so often but only when the app crashes or gets redeployed. I see the advantages of pre-compiling on the client side, but I'm not sure why it's necessary on the server side. Could someone elaborate?

Sourcemap

Would be nice to see sourcemap configuration for use in something like node-inspector

.env is not supported in a preset

Since i updated to latest beta,

    "babel-cli": "^7.0.0-beta.3",
    "babel-core": "^7.0.0-beta.3",
    "babel-node": "^7.0.0-beta.3",
    "babel-preset-env": "^7.0.0-beta.3",

i get this error
image

In my .babelrc I have "presets": ["env", "next/babel"]

I note in the current release v6 everything works fine but not in v7.

following instructions fails. Seems to be problem with .babelrc

When I follow these instructions to the letter, everything works until the step where we move things over to a .babelrc file and remove the options from the scripts. Then I get a

[nodemon] starting `babel-node lib/index.js`
/usr/local/lib/node_modules/babel/node_modules/babel-core/lib/transformation/file/options/option-manager.js:126
      if (!option) this.log.error("Unknown option: " + alias + "." + key, ReferenceError);
                           ^

TypeError: Cannot read property 'error' of undefined
    at OptionManager.mergeOptions (/usr/local/lib/node_modules/babel/node_modules/babel-core/lib/transformation/file/options/option-manager.js:126:28)

Source maps for devel and production with nodemon (solution)

For anyone who has problems getting source maps to work for devel, here is a solution that I just came up with. I am using Node 5.3.

Install the following packages:

$ npm install --save babel-cli babel-preset-es2015 source-map-support
$ npm install --save-dev nodemon

In this example I am using Heroku for production. The reason I put most of the packages in "dependencies" instead of in "devDependencies" to enable Heroku to transpile the js files when it's deployed (so we don't have to check in any dist files). If you don't use Heroku you can use --save-dev for all the packages.

In your index file (index.js, app.js worker.js etc), add:

import 'source-map-support/register';

Important If you have previously used babel-register hook (eg require('babel-register')) make sure to remove it or it will skew the line numbers in stack traces.

package.json - The postinstall script transpiles all js files and put the result in dist/. postinstall will run after any npm install, which Heroku also run when deploying a new version of your app. The start-web and start-worker scripts are for development use and simply tell nodemon to run postinstall (eg. transpiling) on every reload, the transpiled output is then executed.

{
  "scripts": {
    "start-web": "nodemon dist/app.js --ignore dist/ --exec 'npm run postinstall && node'",
    "start-worker": "nodemon dist/worker.js --ignore dist/ --exec 'npm run postinstall && node'",
    "postinstall": "babel *.js -d dist"
  }
}

Now you can npm run start-web locally and it will update as files changes, with the correct line numbers in stack traces:

$ npm run start-web
$ npm run start-worker
$ heroku local # Simulate Heroku env locally (no file watch)

Procfile (Heroku only) - since Heroku automatically run the postinstall npm script at deploy time we can simply use vanilla node to run the transpiled files (eg dist/app.js and dist/worker.js):

web: node --optimize_for_size --max_old_space_size=460 --gc_interval=100 dist/app.js
worker: node --optimize_for_size --max_old_space_size=460 --gc_interval=100 dist/worker.js

.gitignore - make sure to exclude the dist folder before committing and deploying to Heroku:

node_modules
dist

I'm not sure if I am missing something, but this is the only way I could source maps and file watcher to work for production and dev, without using additional tools like gulp or such. Curious to if there is a simpler way (is it possible to use source maps with babel-node for example?).

Wrong example

When getting the error:

Running "babel:dist" (babel) task
Warning: Path must be a string. Received undefined Use --force to continue.

The solution is to change the dist object from src to path
dist: { path: { './server.js': 'src/app.js' } }

At least in versions
"babel-preset-es2015": "^6.24.1", "babel-preset-stage-2": "^6.24.1", "grunt-babel": "^6.0.0"

Also need to change the example.

Is babel-core/register production ready?

Can we do that way in production without falling in performance pit trap?

//  enable runtime transpilation to use ES6/7 in node

/* eslint-disable */
var fs = require('fs');
var path = require('path');

var babelrc = fs.readFileSync(path.resolve(__dirname, '../.babelrc'));
var config;

try {
  config = JSON.parse(babelrc);
} catch (err) {
  console.error('==>     ERROR: Error parsing your babelrc');
  console.error(err);
}
/* eslint-enable */

require('babel-core/register')(config);

got example from https://github.com/bdefore/universal-redux/blob/master/bin/transpile.js

Code coverage with babel

It would be good to make a code coverage tool to work with babel. I have tried to configure both istanbul, babel-istanbul/isparta to get a correct code coverage report, but i havenยดt be able to get the correct code references on the report. I didnยดt even know if the coverage metrics are right.

Could you provide an example for this setup?
There is another coverage tool besides istanbul which plays better with babel?

Thank you!

Babel6 Example

Like all the rest of babels documentation: babel6 and unusuable in babel7 times.

Is there a list with documentation that needs rework? I'd be happy to contribute here and there

Please update readme

Newer version does not need explicit setting of babel-preset-es2015 babel-preset-stage-2. Simply using babel-preset-env in place of babel-preset-es2015 babel-preset-stage-2 will suffice.
Everything else seems fine.
Thanks for writing.

Babel doesn't watch for file change

For now I just restart manually after making changes because babel doesn't automatically transpile original code when changes are made.

So basically I feel getting no benefits from using nodemon from this setup.

Should I use another library for watching purpose?

Run only one script

Hi!

I'm deploying my app in a elastic bean instance so basically I have the option to run just one script.
If I try to run something like:

"start": "npm run build & node script.js"

it's not guaranteed that the first one has finished before the second one starts. Is there an way to pass through that?

Ps: I'm using eb deploy from ElasticBean

Thanks

Webstorm support

Not an issue; just a recommendation (maybe).

If the sourcemaps are modified slightly, you can get Webstorm and Babel to work very well in the same folder. This way, you can potentially avoid the /dist folder. In fact, the Webstorm Debugger works on breakpoints on either the compiled or the es6 files. NOTE: Using find/replace (naive I know), we can make sure that all import '*.es6' file dependencies are changed to the import '*.compiled.js' files.

screenshot 2016-02-21 23 49 22

LinkedInprofile

refId=4GFnsQoznrdysdG5rxPtYA%3D%3D&trackingId=%2F7GLnoCt9PfJM4J3oPSOaw%3D%3D&position=17&pageNum=0&trk=public_jobs_jserp-result_search-card

On DigitalOcean ubuntu ..

... after npm install --save-dev babel-cli babel-preset-env
babel is still not available in CL. Instead i see below output.

root@socket-server-ZZ:~/projects/my_app# babel src -d dist/
The program 'babel' can be found in the following packages:
 * babel-1.4.0
 * openbabel
Try: apt install <selected package>

Add example of importing local file from a different path

Say index.js imports a date utility function from ../utils/date. If we run babel-node lib/index/js, that will work. But the serve script, node dist/index.js will no longer work, because utils/date.js has an export statement, which Babel didn't transpile.

We could have utils/src/date.js transpiled to utils/lib/date.js, but then index.js needs to be changed to explicitly import utils/lib/date, and babel-node lib/index.js alone will no longer work correctly for development because utils/lib/date.js is outdated vs. utils/src/date.js.

@thejameskyle, what would be the best practice here?

similar example for cli/bin environment?

Would be useful to me. Where a project would normally use something like:

  "bin": {
    "my-app": "bin/my-app.js"
  },

You're not using nodemon or an npm run script. Would you just not compile the entry point script?

We could add to this README or add another example-node-cli or something of the sort.

Change port and test fails.

First of all... thanks. ๐Ÿ‘Œ๐Ÿฟ

I had modified the port, now it is running on port 8000. Also, I had to change the test final route, now is pointing to the 8000 and not into the
http.get('http://127.0.0.1:1337', res => {
.

And everything looks fine, except that when I run the test and log the output to check if it is okay, it returns that is okay... but assert doesn't notice it.

`

server on port 8000 ready
  Example Node Server
๐Ÿ™…๐Ÿปโ€โ™‚๏ธ 200
    1) should return 200


  0 passing (30ms)
  1 failing

`

It does not resolve import/export

Hi, thanks for you example.

I have a question. When I added my additional module via

import test from './test.jsx';

with content

export default 1;

babel compile it to

var _test = require('./test');

var _test2 = _interopRequireDefault(_test);

and it does not work because node does not understand export default 1;.

Babel somehow resolve dependencies for production? Or I should use webpack for example?

Deployment

I love the set up but I was wondering where can I deploy a node server like this one ?

Script in "Watching file changes with nodemon" doesn't work

Please correct me if I interpret the section "Watching file changes with nodemon" wrong.

What I found in this section is that after issue npm start, the project won't rebuild when index.js is modified, i.e. when I modified the greetings, it was not updated when I refresh the page. The script seems only to build the project once when the command is issued.

Following script works for me:

"start": "nodemon --exec \"npm run build && node dist/index.js\" -e js --ignore dist/"

I have some questions to ask you about

In the section of 'Watching file changes with nodemon', I find something wrong. If change file 'index.js', nodemon will indeed restart the server. But the 'build' script will not rerun automatically. So if we refresh the browser, we will find the page will not change as we expected.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.