How to run server written in js with Node.js

前端 未结 6 1234
梦毁少年i
梦毁少年i 2020-12-24 12:33

I have installed node.js from here http://nodejs.org/ . in my windows8 machine. copied the example server code in my server.js file

var http = require(\'ht         


        
相关标签:
6条回答
  • 2020-12-24 13:02

    You don't need to go in node.js prompt, you just need to use standard command promt and write

    node c:/node/server.js
    

    this also works:

    node c:\node\server.js
    

    and then in your browser:

    http://localhost:1337
    
    0 讨论(0)
  • 2020-12-24 13:08

    I open a text editor, in my case I used Atom. Paste this code

    var http = require('http');
    http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Hello World\n');
    }).listen(1337, '127.0.0.1');
    console.log('Server running at http://127.0.0.1:1337/');
    

    and save as

    helloworld.js
    

    in

    c:\xampp\htdocs\myproject 
    

    directory. Next I open node.js commamd prompt enter

    cd c:\xampp\htdocs\myproject
    

    next

    node helloworld.js
    

    next I open my chrome browser and I type

    http://localhost:1337
    

    and there it is.

    0 讨论(0)
  • 2020-12-24 13:15

    If you are in a Linux container, such as on a Chromebook, you will need to manually browse to your localhost's address. I am aware the newer Chrome OS versions no longer have this problem, but on my Chromebook, I still had to manually browse to the localhost's address for your code to work.

    To browse to your locahost's address, type this in command line: sudo ifconfig

    and note the inet address under eth0.

    Otherwise, as others have noted, simply type node.js filename and it will work as long as you point the browser to the proper address.

    Hope this helps!

    0 讨论(0)
  • 2020-12-24 13:26

    Just try

    node server
    

    from cmd prompt in that directory

    0 讨论(0)
  • 2020-12-24 13:27

    Just go on that directory of your JS file from cmd and write node jsFile.js or even node jsFile; both will work fine.

    0 讨论(0)
  • 2020-12-24 13:29

    Nodejs is a scripting language (like Python or Ruby, and unlike PHP or C++). To run your code, you need to enter a command in the terminal / shell / command prompt. Look for an application shortcut in your operating system by one of those names.

    The command to run in the terminal will be

    node server.js
    

    But you will first need to browse in the terminal to the same folder as the file server.js. The syntax for using the terminal varies by operating system, look for its documentation.

    0 讨论(0)
提交回复
热议问题