search

Home  >  Q&A  >  body text

javascript - Just want to set up a simple server using node

First of all, my nodejs installation path is under C:\Program Files\nodejs,
My project is built under the D:\node folder
project There is a server.js file used to create a server. The content is as follows:

var http = require('http');//使用 require 指令来载入 http 模块

http.createServer(function (request, response) {

    // 发送 HTTP 头部 
    // HTTP 状态值: 200 : OK
    // 内容类型: text/plain
    response.writeHead(200, {'Content-Type': 'text/plain'});

    // 发送响应数据 "Hello World"
    response.end('Hello World\n');
}).listen(8888);

// 终端打印如下信息
console.log('Server running at http://localhost:8888/');

How to start this server? ? ?
I entered in cmdC:\Program Files\nodejs\node D:\node\server.js
The prompt reported an error: SyntaxError: Invalid or unexpected token
I tried many methods None of them work, please help me find out where the problem is

为情所困为情所困2827 days ago394

reply all(7)I'll reply

  • 某草草

    某草草2017-05-16 13:24:09

    Try it node D:nodeserver.js

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-16 13:24:09

    Isn’t it necessary to start the service? node server.js

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 13:24:09

    You can enter directlyD:node,然后按shift键右击选择cmd窗口打开,直接运行node server.jsThat’s it

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-16 13:24:09

    After running it, there is no problem with the code of the question. Is there something wrong with the node program? Run it and try something else

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-16 13:24:09

    The SyntaxError: Invalid or unexpected token error is a syntax error. Check whether there are unmatched brackets, redundant or missing punctuation marks. Or use Chinese punctuation marks

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-16 13:24:09

    Method 1:

    Method 2:

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 13:24:09

    When I see an error report, I just panic and don’t know to read the error message carefully. I just shout to God, God, come and help me. I can’t do it. What should I do if the program reports an error?

    No one is born a master, others are just more careful and patient than you.

    Even the syntax error cannot be solved by itself. Segmentfault is really unskilled.

    reply
    0
  • Cancelreply