How to test express api on Travis - node.js

As mention in a previous post, the following lines only works on local machine for testing.
node app.js
karma start karma.conf.js --single-run
If I put it into the .travis.yml, it make Travis hanging on the "node app.js" line and fail the test after timeout. I think this is because node is supposed to start the app.js and keep listening. On the local machine, I can open 2 terminal windows and run them separately but I am not sure how to do it on Travis. Can anyone help?

node app.js & will make Travis run app.js in the background (this should also work locally). The "&" bit is a standard piece of shell syntax to make a process run in the background.

Related

npm-run-all failing when running a "tsc-watch" command in parallel

I am trying to use npm-run-all to manage running two servers at the same time. For one of the servers, I am trying to run it in a watch mode using tsc-watch.
The command looks like such:
"start": "tsc-watch --onSuccess \"node dist/server.js\""
npm-run-all starts up both of the servers successfully. tsc-watch also watches the Typescript file for changes and automatically recompiles it successfully.
However, when tsc-watch recompiles the Typescript file and restarts the server, it seems like npm-run-all stops working properly. Specifically, if I try to kill both of the servers by pressing CTRL + C in the terminal (OSX), it will only kill the server that tsc-watch recompiled while the other server remains running.
I'm thinking that there must be a way to fix this. Anyone have some tips for me?
Found an answer. I think this is just a bug with npm-run-all. Using concurrently instead doesn't experience this issue (kills both servers when hitting CTRL-C).

Waiting for webpack bundling before launching nodemon

I have a nodejs project written in Typescript. Therefore, i have webpack using a typescript loader that transpiles my code in Javascript and bundles it in a server.js file (in a dist folder)
When in developement conditions, my webpack runs with its watcher ON and so does nodemon.
Problem is, when i launch my script for the first time combining webpack and nodemon, since webpack is in watch mode it doesn't have an exit code saying that everything is ok, nodemon script can be started. If i run them simultaneously, nodemon will launch faster than webpack and since server.js file doesn't yet exist, it will crash at the start.
I want to launch thanks to one single command both scripts but make nodemon command wait for the bundling to be done.
First of all, when please provide some code when submitting questions.
and since server.js file doesn't yet exist
I think you should work around your setup a little bit s.t. webpack doesn't create your server.js file if you want to do this.
Basically you can chain multiple commands in a script like so webpack -d && nodemon index.js. This will launch node after webpack completes. However if you setup webpack in watch mode -w it never exists, so you can't chain another command to it. So webpack -d -w && nodemon index.js never gets to the nodemon part.
A solution to the above is to chain them using only &, which I guess you are doing, but in this way they both start at the same time. Hence, if you make your setup independent (webpack doesn't interfere with nodemon starting script) you can list them like so.
If for whatever reasons you can't do this or don't want to, your only option is with 2 separate scripts that you launch manually one after the other.
If I were you, I would just use nodemon-webpack-plugin:
Uses Nodemon to watch and restart your module's output file, but only
when webpack is in watch mode (ie, --watch).
Saves the need for installing, configuring and running Nodemon as a
seperate process.

Why server restart is needed in Node.Js for every change?

I am very new in Node.Js. I just started node.js basic tutorial. But when I change my code I have to restart the server all the times. But is there any way where no need to restart the server again and again.
Nodemon is the best solution for this.
Install nodemon like this "npm i nodemon"
Then restart your project with nodemon, "nodemon app"
You are good to go...
You can install node-supervisor to restart automatically your server when you change the code.
I'm not sure on the details of the compilation process. But I think it's correct to say that on app start, your source code is parsed into computer instructions represented in memory and executed. During runtime source code files are not re-parsed. And so changing the source code will have no effect on the running application. Unless the application re-parses a file prior to execution of the code in that file. Possibly a service worker... But I'm not sure and that would be an exception.
A good way of thinking of nodejs and javascript files (imo) is that the javascript files are configuration for nodejs. Which is a c++ app. So if the configuration changes you need to restart node to read the new configuration.
There are tools such as nodemon that will monitor the source code for file saves and trigger the node application to restart.
Check out Nodemon.
nodemon will watch the files in the directory in which nodemon was started, and if any files change, nodemon will automatically restart your node application.
nodemon does not require any changes to your code or method of development. nodemon simply wraps your node application and keeps an eye on any files that have changed. Remember that nodemon is a replacement wrapper for node, think of it as replacing the word "node" on the command line when you run your script.

I cannot use npm start to start an express server on windows

I am new to node. I am trying to run an express server. I get no errors when installing express but when i run npm start or node app (as all the beginner tutorials point) nothing seems to be happening. The only way i can start the server is by typing node /bin/www. My operating system is Windows. Any advice?
The Express scaffold script generates a package.json file with a start script field that points to the app.js file it also created. Since Express 4 was released the scaffold generator script has been moved to its own package. https://github.com/expressjs/generator
All npm start does is look in the package.json file for a starting script to pass to node. You can see this in the documentation.
Running npm start with a package.json like this:
"scripts": {
"start": "app.js"
}
Is exactly equivalent to running node app.js.
I managed to solve my issue by changing the code page of cmd-dos. By using chcp 850 or chcp 65001 in cmd thus changing codepage to latin - utf8 the issue is gone.

How to run tests with gruntJS

So, how do I run the test suite in gruntjs? I thought it should be pretty simple and straight-forward but well, for me it wasn't :)
Since gruntjs should be able to run tests I thought that I could just run "grunt test", but that for some reason requires the server to run. I tried starting it in another process with "grunt server watch" but then again the "grunt test" fails.
How is grunt supposed to work?
'grunt test' is a task that starts server too when required. It starts a test runner, for example karma and karma runs the tests in browser environment.
The trick is to run karma and provide configuration items in karma.conf.js
A sample gruntfile on gruntjs website shows how to make qUnit work with grunt.

Resources