What I want is simply refresh/reload the application when some change is made on the code. For now, I have to stop the server (ctrl + c on the terminal) and then run it again so the changes have any effect
Nest has a watch mode for this. Simply run npm run start:dev and nest will react to any change
Related
I'm building an E-commerce site, where there's an Authentication system.
I noticed that if the client login with a wrong user or password, the backend/server that works with nodemon will crach and hang in there crashed till i restart manually nodemon. This is example output error of the nodemon crash:
[nodemon] app crashed - waiting for file changes before starting...
node:internal/errors:464
ErrorCaptureStackTrace(err);
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent
to the client
Ofcourse, when server crashes, client can no more access or do login again till server restarts.
After some googling, i found this question and this repository that fix my problem but particulary and not as expected precisely, i dont want nodemon to restart forever on any error that occure ofcourse, but only with specifics errors that i set them -like Authentication errors as i mentionned above-.
So, my idea/question is: is there anyway to get nodemon restarts by itself in some cases of failures or errors (NOT ALL)?
Seems like you a referring to a production situation, and nodemon is a development node server, which is not intended for use in production, as the intro states:
nodemon is a tool that helps develop Node.js based applications by
automatically restarting the node application when file changes in the
directory are detected.
You should use node.js in production, instead of nodemon.
For managing your node server in production, you could use a process manager like PM2..
That said, an authentication server that crashes every time a user uses a wrong password seams very ineffective in handling a common use case. So I would advise to start with fixing the root cause, which is the buggy server, and then for recovery from incidental crashes use something like PM2.
PS:
The error you are getting looks like an express error you get when you send a response (in this case an error response) without exiting the function e.g. by using return. Because you are not returning, another res.send is called, which causes the 'ERR_HTTP_HEADERS_SENT' error. See this answer.
This is really bad since it can send your program into a loop of restarting, but if you really want it, replace app.js with your file's name and try this:
nodemon -x 'node app.js || copy /b app.js +,,'
Linux version:
nodemon -x 'node app.js || touch app.js'
Next time try a little googleing before you ask since it is most likely faster.
this is my first time working with node, I set up a project and used the command:
npx tailwindcss -i styles.css -o ./tailwind/output.css --watch
to start the build process for the tailwindcss classes, but this stops the VS Code live server extension from refreshing the webpage, it is supposed to refresh everytime I save the html file, but now I have to manualy go into the browser and refresh it, which defeats it's purpose. Only after I type CTRL + C (^C) in the terminal, does it work, but in that case the tailwindcss rebuilding stops. Has anyone encountered this before or is this a beginner mistake?
go to this address in the VS Code :
File > Preferences > Settings, then search live-server. scroll down and check the Full Reload. setting Full Reload to true solves the issue.
If full reload is not wanted, a longer delay may work "liveServer.settings.wait" : 300 in /.vscode/settings.json. If sometimes fail, a second save will inject the new css.
The problem was solved for me by setting liveServer.settings.fullReload": true in /.vscode/settings.json.
I currently have a dev-setup with node-dev and iIrecently integrated tsoa into the setup to generate a swagger file and the express-routes for my app.
I already added all the necessary steps to generate my routes but right now its a manual process inside the dev-setup because I cant execute the needed npm-task on every restart of the application through node-dev.
So my question is, if its possible to start this task on every restart initiated by node-dev?
I hope someone can help me, and thanks in advance.
In your package.json add the following entry to scripts
"profit": "tsoa routes && node-dev -r tsc/register app.ts"
Then run it with
npm run profit
You can change profit to whatever you want
I've looked around and had a lot of trouble figuring this out. I'm hoping someone might be able to point me to a post or have information on how to do this.
My problem is that I have 2 projects I've made using WebStorm:
I have 1 application that is my server-side code running on port 3000. It's a simple Node Express app.
The second application is an Angular 4 / Ionic 3 application running the client side on port 8100.
I want to run my server application in debug mode, so that it hits the breakpoints for all the data being sent from the client side app.
For example: Angular / Ionic app sends a get request for all clients for a given customer. The customer is sent via url parameter. I want the server code to pause when it receives this request and so I can see this URL parameter. Fairly simple.
The server is also using grunt to build the project, and nodemon to watch it. I'm using some npm scripts to make life easy. Here are the scripts:
"scripts": {
"dev": "SET NODE_ENV=development && nodemon ./bin/www",
"grunt": "grunt",
"start": "node ./bin/www"
},
Nothing fancy.
I have WebStorm configured to run my scripts from hitting play. So the play button will first run the following sequence:
npm run grunt
npm run dev
Again ... nothing fancy.
Now how do I get this thing to setup a debugger so I can listen in WebStorm? I have a both projects open in separate windows, and I am initiating the calls to the server from the client. How do I make the break points grab hold and show me the data coming into the server?
I feel like this is incredibly easy and I'm missing something really stupid. Any help would be much appreciated.
You need starting you server in debugger to get breakpoints in your server code hit. If you prefer to start your app via npm script, you have to add $NODE_DEBUG_OPTION (or %NODE_DEBUG_OPTION% on Windows) to make sure that Node.js is started with appropriate debug options (--debug-brk, --inspect-brk, etc)
So:
in package.json, modify your dev script as follows:
"dev": "SET NODE_ENV=development && nodemon %NODE_DEBUG_OPTION% ./bin/www"
right-click your package.json, choose Show npm scripts
right-click dev script in npm tool window that opens, choose edit 'dev' settings to create a run configuration.
open your source files in editor, add breakpoints
press Debug to start debugging
run your client app, initiate the calls to the server
I am working in the development on my mac laptop. Here are what I need to do to start the development environment:
1) open console, 'sudo mongod' - start mongoDB server
2) open another console, 'mongo' - open mongo console for debug CRUD process
3) open another console, 'cd /projects/myProject', 'npm start' - start the nodejs api service on localhost
4) open another console, and 'cd /projects/myProject', 'gulp' - start the gulp browserify & watch process
5) open another console, 'cd /projects/myProject/build', 'httpster' - start a mini http server for front-end built code, so that I can access it in browser at 0.0.0.0: 3333
If you do those everyday, it is not fun at all. I think it is a common problem which is very likely to have a solution.
Is there anyway to complete all above by executing one single js file? e.g.: node start.js.
If would be best if you can provide some demo code.
Thank you!
Nick
You can use gulp-shell. I use it to start mongoDB from gulp.