Home > Web Front-end > JS Tutorial > body text

Node.js first try cry

黄舟
Release: 2017-01-17 15:32:03
Original
1284 people have browsed it

Check node version

[code]node -v
Copy after login

Create the first nodejs program

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

When running, the file name is case-free

Interactive mode:

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

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端口')
Copy after login

Terminal operation:

[code]node server.js
Copy after login

Browser running:

[code]http://localhost:8888/server.js
Copy after login

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)!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!