0

Is it possible to pause the startup of a nodeJS server and ask the operator to enter some text before the server is finally fully started?

Background: I want to implement user authentication, and therefore use a token based system. To encode/decode those tokens, a seed (or secret) is required.

Since I don't really want to store that seed somewhere on the server, I was thinking about asking for it when the server starts, and then store it in a variable.

I know that I can pass the secret as a parameter on start, and it works that way, but then it's shown in the console. I'd love to have a prompt for the secret, without showing it (or maybe showing ****).

Is there a way to do that?

nim
  • 35
  • 4

3 Answers3

1

Yes, using the readline module https://nodejs.org/api/readline.html

Efi Shtainer
  • 394
  • 3
  • 8
0

It depends. You could freeze the thread using setTimeout or similar but it's not recommended (by me as I don't know if that would have any implications). You can also stop the stream flow, which I guess would be better suited, you may stop/start if token is present or not. Check this documentation for more details.

To prompt you may use something like prompt module, alternatively if you don't wnat to import a lot of stuff for a simple task you can do something like this

function ask(q, c) {
    var sin = process.stdin;
    var sout = process.stdout;

    sout.write(q);

    sin.once('data', function (data) {
        callback(data.toString().trim());
    });
}

To use it you would do a simple call with a callback to that function

ask('Token?', function (token) {
    console.log(token);
    process.exit();
});

variable process is a default nodejs process functionality

Another good resource is the one Efi Shtainer posted.

Adrian
  • 8,271
  • 2
  • 26
  • 43
0

You can use readline and 'prompt' the user for some input, and then 'start the server' (what kind of service?) from a callback after you have recieved said input.

callback guide : nodeJs callbacks simple example

Rheijn
  • 319
  • 3
  • 11