How to stop the preview server - node.js

This code is based on wintersmith static site generator
In my code I'm starting the preview server like this:
env.preview(function(error, server) {
if (error) throw error;
console.log('Server running!');
setTimeout(function(){
console.log("going to shut it down")
server.stop();
}, 3000)
});
This is taken from the example. I would expect the preview server to stop after 3 seconds, but instead I get an error, that server is undefined.
I looked a bit closer at it and indeed: server is undefined. Anything I'm doing wrong? How am I able to stop the preview server?

This looks like a bug, create an issue on github and i'll have a look at it when i get some time.

Related

Nodejs on azure : server not starting before the first request

I have a weird probleme, when starting listening I run the following function :
app.listen(process.env.PORT || 3000, async function () {
await db.init().catch(err => {
console.error(err);
console.error("Shutting down because there was an error setting up the database.");
process.exit(1);
});
await recordsDb.init().catch(err => {
console.error(err);
console.error("Shutting down because there was an error setting up the records database.");
process.exit(1);
});
//this db object will be used by controllers..
app.set('db',db);
});
the problem is that the function doesn't run before the first request (and so the first request always fail), am I missing something ?
Thank you!
You will need to first scale up your Web App Service plan, then you can enable Always On. By default, apps are unloaded if they are idle for some period of time. This lets the system conserve resources. In Basic or Standard mode, you can enable Always On to keep the app loaded all the time. If your app runs continuous WebJobs or runs WebJobs triggered using a CRON expression, you should enable Always On, or the web jobs may not run reliably. Free Azure web apps do not support Always On.
https://learn.microsoft.com/en-us/azure/app-service/web-sites-configure

Does console.log not work within socket.io event?

Is there something that prevents a console.log() from firing within a socket.io event? I am having a frustrating time with them.
socket.on('messageReceived', function(data) {
console.log('Server ' + data.msg);
var clientData = {'msg', 'Hello server'};
socket.emit('clientResponse', clientData);
});
in that code my node server receives the client response, but my browser console does not output the console.log. I don't get why... is this normal?
You can try console.log('a') in console of browser.
If you don't see a, I think you changed level log of console (verbore, info, warn, error) or used filter (you can change it in under of tab console).
I don't know what happen if console priint a.
I figured out the problem. I was using socket.broadcast.emit from the server instead of broadcast.emit.
I noticed when another browser I had open outputted the console.log.
Thanks everyone who read and put thought into this.
I also had console.log not working and I have been spending the weekend debugging it. Nothing worked. (But my server was still running fine) Finally, I became fed up with it and I commented out my entire io.on('connection') code and it still ran.
Then, I went to my client and I saw that I still had my client sending and retrieving to and from my Heroku build that I set when I published the website.
So if you're having issues and nothing that you do is working, check the socket server that you're connecting to with your client.

Is there a way to log everything from console.log in an Electron app using electon-log?

I already have a ton of console.log statement thru out the main process of my Electron app, is there a way to send all of those to electron-log or another log that can be access after the app is packaged? I was going to go thru and add log.error thru out the script, but hoping there is a better way.
console.log('this error', error)
log.error('this error:', error)
Something like:
console.log = function(){
...your code...
};
Can be an idea?

Gcloud crashes with a time data error when using “gcloud app logs read” command

I’m encountering an error that I’ve never seen before when I run “gcloud app logs read” after deploying my app using "gcloud app deploy".
This is the error: “ERROR: gcloud crashed (ValueError): time data '2016-09-16T19:39:42Z' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'”
I thought it might be because of some changes I made to the app, but I tried deploying a simple server and I’m still getting the same error when I try to look at the logs. Again, it does seem to be deploying, so I’m having trouble figuring out what the problem is with the logging functionality.
IOW, no matter what I deploy, I get the same error.
I get the same error when I deploy this:
'use strict';
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.status(200).send('Hello, world!');
});
// Start the server
var server = app.listen(process.env.PORT || '8080', function () {
console.log('App listening on port %s', server.address().port);
console.log('Press Ctrl+C to quit.');
});
If anyone has any ideas, it would be much appreciated. Thank you.
Edit: Added code.
This is related to a known issue and should be fixed shortly. You can check the logs in the Cloud Console, or using the gcloud beta logging read command instead in the meantime.
Update: we have a fix expected in the next Cloud SDK release.
Update: Cloud SDK 127.0.0 is out; please update your client and this should be resolved.
'2016-09-16T19:39:42Z' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'
Somewhere in your code, you are asking to show a datetime string with microseconds: .%f. But, the datetime element you are providing does not have the microseconds. Either remove the .%f, or make the datetime string have microseconds.

Node.js server resets on user error

I’m developing a Node.js application which in a user signs up in three levels.
the problem is when a user encounters and error in any of the levels, the whole server reset and the rest of the user sessions get close.
the reason that server restarts is that I’m using forever start app to start my app,otherwise it goes down completely.
how to stop the server to stops completely when just one users
encounters any error?
how to start each users in an individual thread(by thread i mean an isolate environment)?
Considering #Michael 's answer
unfortunately that's not how Node works. An unhandled error can cause
the server to restart.
The only way is to handle execptions in a proper way. So you can check this answers comments about how to handle an uncaughtException
var handleRequest = function(req, res) {
res.writeHead(200);
res1.end('Hello, World!\n');
};
var server = require('http').createServer(handleRequest);
process.on('uncaughtException', function(ex) {
// do something with exception
});
server.listen(8080);
console.log('Server started on port 8080');

Resources