Ubuntu 18.04安装node.js并创建第一个应用

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/m0_43404744/article/details/94364508

1、安装node.js

sudo apt-get install nodejs
sudo apt-get install npm

2、创建 node.js 应用

创建文件helloworld.js,并在文件中输入下列内容。

var http = require('http');//我们使用 require 指令来载入 http 模块,并将实例化的HTTP赋值给变量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(3000); //使用 listen 方法绑定3000端口
// 终端打印如下信息
console.log('Server running at http://localhost:3000/');

3、运行node.js应用

在终端输入命令node server.js
终端显示Server running at http://localhost:3000/
访问http://localhost:3000/,出现下列页面:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_43404744/article/details/94364508