How to run server written in js with Node.js

前端 未结 6 1253
梦毁少年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: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.

提交回复
热议问题