Home  >  Article  >  Web Front-end  >  Node.js first try cry

Node.js first try cry

黄舟
黄舟Original
2017-01-17 15:32:031297browse

Check node version

[code]node -v

Create the first nodejs program

[code]helloWorld.js
console.log("Hello World");

When running, the file name is case-free

Interactive mode:

Enter at the command line:
node
[code]> console.log('hello');

Create the first application

Steps:

1. Use require to introduce the module HTTP

2. Create a server http.createServer()

3. Accept and respond to requests

server.js
[code]var http = require('http');

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

    // 发送响应数据---浏览器输出
    response.end('Hello World');
}).listen(8888);    // 监听8888端口
// 终端输出
console.log('服务器监听8888端口')

Terminal operation:

[code]node server.js

Browser running:

[code]http://localhost:8888/server.js

Node.js first try cry

##The above is the first try of Node.js Content, for more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn