Pergunta

I am new to Node and vim.

I have the following code file, server.js open in vim:

var http = require("http");

function onRequest(request, response) {
    console.log("Request received");
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Hello World");
    response.end();
}

http.createServer(onRequest).listen(8888);

console.log("Server has started");

When I enter vim "command" mode and type:

:!node server.js

Things work as expected (I think). The window changes to the standard terminal window, and if I open a browser and navigate to localhost on port 8888, the silly "Hello World" text is displayed.

The terminal displays the "Server has started" text, and the output that the request was received. All is well.

When I close the browser window, and type Ctrl-z to stop the Node server, the terminal displays [1}+ stopped vim server.js, but does not return me to my code file in vim.

If I return to vim by typing vim, vim opens into the same directory, but my file is not open. If I try to open the file, vim complains about a duplicate swap file. Obviously I can use the [R]ecover option, but it seems like I am missing a step.

What am I missing here?

I have googled, and searched SO, but I am not sure what I am asking for beyond the title of this question (which has not produced the answer I am looking for, or if it did, I didn;t recognize it). I am just new enough that I am unsure if I am missing something about the terminal, or vim, or *nix.

Thanks in advance . . .

UPDATE: It occurs to me I never clearly state what I am after - I am wanting to stop the server in the terminal and return to the open file in vim. As Tadman mentions below, I knew I was probably attempting to open an additional editor instance. What I was unclear on was how to return to the existing instance. Trying Tadman's suggestion now.

Foi útil?

Solução

You're creating a sub-shell to run node, suspending that with ^Z and then open a new copy of vim when you do your next step. Now you have two editors running, so you get an error about the swap file.

Why not either do this in a separate shell, which is what most people tend to do, or use ^C to stop the server and return to your editor?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top