{"id":13416,"date":"2023-12-25T15:43:55","date_gmt":"2023-12-25T07:43:55","guid":{"rendered":"https:\/\/www.zhidianwl.net\/zhidianwl\/?p=13416"},"modified":"2023-12-25T15:43:55","modified_gmt":"2023-12-25T07:43:55","slug":"ubuntunodejs%e5%bc%80%e5%8f%91%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a1%b9%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.net\/zhidianwl\/2023\/12\/25\/ubuntunodejs%e5%bc%80%e5%8f%91%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a1%b9%e4%bb%8b%e7%bb%8d\/","title":{"rendered":"ubuntunodejs\u5f00\u53d1\u6ce8\u610f\u4e8b\u9879\u4ecb\u7ecd"},"content":{"rendered":"

Ubuntu\u662f\u4e00\u79cd\u57fa\u4e8eDebian\u7684Linux\u64cd\u4f5c\u7cfb\u7edf\uff0c\u662f\u5f00\u6e90\u8f6f\u4ef6\u7684\u4ee3\u8868\u4e4b\u4e00\u3002Node.js\u662f\u4e00\u4e2a\u57fa\u4e8eChrome V8\u5f15\u64ce\u7684JavaScript\u8fd0\u884c\u65f6\u73af\u5883\uff0c\u7528\u4e8e\u6784\u5efa\u5feb\u901f\u3001\u53ef\u6269\u5c55\u7684\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728Ubuntu\u4e0a\u8fdb\u884cNode.js\u5f00\u53d1\u3002<\/p>\n

\u5b89\u88c5Node.js<\/p>\n

\u5728Ubuntu\u4e0a\u5b89\u88c5Node.js\u975e\u5e38phpwamp\u6253\u5305exe<\/a>\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u5373\u53ef\uff1a<\/p>\n

“`<\/p>\n

sudo apt-get update<\/p>\n

sudo apt-get install nodejs<\/p>\n

“`<\/p>\n

\u6b64\u5916\uff0c\u8fd8\u9700\u8981\u5b89\u88c5npm\uff08Node.js\u7684\u5305\u7ba1\u7406\u5668\uff09\uff1a<\/p>\n

“`<\/p>\n

sudo apt-get install npm<\/p>\n

“`<\/p>\n

\u521b\u5efa\u4e00\u4e2aNode.js\u5e94\u7528\u7a0b\u5e8f<\/p>\n

\u5728Ubuntu\u4e0a\u521b\u5efa\u4e00\u4e2aNode.js\u5e94\u7528\u7a0b\u5e8f\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u76ee\u5f55\uff0c\u5e76\u8fdb\u5165\u8be5\u76ee\u5f55\uff1a<\/p>\n

“`<\/p>\n

mkdir myapp<\/p>\n

cd myapp<\/p>\n

“`<\/p>\n

\u7136\u540e\uff0c\u4f7f\u7528npm\u521d\u59cb\u5316\u9879\u76ee\uff1a<\/p>\n

“`<\/p>\n

npm init<\/p>\n

“`<\/p>\n

\u8be5\u547d\u4ee4\u5c06\u5f15\u5bfc\u60a8\u5b8c\u6210\u521b\u5efapackage.json\u6587\u4ef6\u7684\u8fc7\u7a0b\uff0c\u8be5\u6587\u4ef6\u5305\u542b\u5e94\u7528\u7a0b\u5e8f\u7684\u5143\u6570\u636e\u548c\u4f9d\u8d56\u9879\u3002<\/p>\n

\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aapp.js\u7684\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n

“`<\/p>\n

const http = require(‘http’);<\/p>\n

const hostname = ‘127.0.0.1’;<\/p>\n

const port = 3000;<\/p>\n

const server = http.createServer((req, res) => {<\/p>\n

res.statusCode = 200;<\/p>\n

res.setHeader(‘Content-Type’, ‘text\/plain’);<\/p>\n

res.end(‘Hello World\\n’);<\/p>\n

});<\/p>\n

server.listen(port, hostname, () => {<\/p>\n

console.log(`Server running at http:\/\/${hostname}:${port}\/`);<\/p>\n

});<\/p>\n

“`<\/p>\n

\u8be5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2aHTTP\u670d\u52a1\u5668\uff0c\u5e76\u5728\u7aef\u53e33000\u4e0a\u4fa6\u542c\u8bf7\u6c42\u3002\u5f53\u670d\u52a1\u5668\u6536\u5230\u8bf7\u6c42\u65f6\uff0c\u5b83\u5c06\u8fd4\u56de\u201cHello World\u201d\u3002<\/p>\n

\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f<\/p>\n

\u8981\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\uff0c\u8bf7\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n

“`<\/p>\n

node app.js<\/p>\n

“`<\/p>\n

\u60a8\u5e94\u8be5\u4f1a\u770b\u5230\u4ee5\u4e0b\u8f93\u51fa\uff1a<\/p>\n

“`<\/p>\n

Server running at http:\/\/127.0.0.1:3000\/<\/p>\n

“`<\/p>\n

\u73b0\u591a\u4e2aexe\u6587\u4ef6\u7ed1\u6210\u4e00\u4e2aexe<\/a>\u5728\uff0c\u60a8\u53ef\u4ee5\u5728Web\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00http:\/\/127.0.0.1:3000\/\uff0c\u5e76\u5e94\u8be5\u770b\u5230\u201cHello World\u201d\u6d88\u606f\u3002<\/p>\n

\u5b89\u88c5Express\u6846\u67b6<\/p>\n

Express\u662f<\/p>\n

<\/figure>\n<\/p>\n

\u4e00\u4e2a\u6d41\u884c\u7684Node.js Web\u5e94\u7528\u7a0b\u5e8f\u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u8f7b\u677e\u5730\u6784\u5efaWeb\u5e94\u7528\u7a0b\u5e8f\u3002\u8981\u5b89\u88c5Express\uff0c\u8bf7\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n

“`<\/p>\n

npm install express –save<\/p>\n

“`<\/p>\n

\u521b\u5efa\u4e00\u4e2aExpress\u5e94\u7528\u7a0b\u5e8f<\/p>\n

\u521b\u5efa\u4e00\u4e2aExpress\u5e94\u7528\u7a0b\u5e8f\u4e0e\u521b\u5efa\u4e00\u4e2aNode.js\u5e94\u7528\u7a0b\u5e8f\u7c7b\u4f3c\u3002\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u76ee\u5f55\uff0c\u5e76\u8fdb\u5165\u8be5\u76ee\u5f55\uff1a<\/p>\n

“`<\/p>\n

mkdir myapp<\/p>\n

cd myapp<\/p>\n

“`<\/p>\n

\u7136\u540e\uff0c\u4f7f\u7528npm\u521d\u59cb\u5316\u9879\u76ee\uff1a<\/p>\n

“`<\/p>\n

npm init<\/p>\n

“`<\/p>\n

\u63a5\u4e0b\u6765\uff0c\u5b89\u88c5Express\uff1a<\/p>\n

“`<\/p>\n

npm install express –save<\/p>\n

“`<\/p>\n

\u7136\u540e\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aapp.js\u7684\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n

“`<\/p>\n

const express = require(‘express’);<\/p>\n

const app = express();<\/p>\n

app.get(‘\/’, (req, res) => {<\/p>\n

res.send(‘Hello World!’);<\/p>\n

});<\/p>\n

app.listen(3000, () => {<\/p>\n

console.log(‘Example app listening on port 3000!’);<\/p>\n

});<\/p>\n

“`<\/p>\n

\u8be5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2aExpress\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u5728\u7aef\u53e33000\u4e0a\u4fa6\u542c\u8bf7\u6c42\u3002\u5f53\u670d\u52a1\u5668\u6536\u5230\u8bf7\u6c42\u65f6\uff0c\u5b83\u5c06\u8fd4\u56de\u201cHello World\u201d\u3002<\/p>\n

\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f<\/p>\n

\u8981\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\uff0c\u8bf7\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n

“`<\/p>\n

node app.js<\/p>\n

“`<\/p>\n

\u60a8\u5e94\u8be5\u4f1a\u770b\u5230\u4ee5\u4e0b\u8f93\u51fa\uff1a<\/p>\n

“`<\/p>\n

Example app listening on port 3000!<\/p>\n

“`<\/p>\n

\u73b0\u5728\uff0c\u60a8\u53ef\u4ee5\u5728Web\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00http:\/\/localhost:3000\/\uff0c\u5e76\u5e94\u8be5\u770b\u5230\u201cHello World\uff01\u201d\u6d88\u606f\u3002<\/p>\n

\u603b\u7ed3<\/p>\n

\u5728Ubuntu\u4e0a\u8fdb\u884cNode.js\u5f00\u53d1\u975e\u5e38\u7b80\u5355\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528npm\u5b89\u88c5\u5404\u79cd\u5305\uff0c\u4f8b\u5982Express\u6846\u67b6\uff0c\u4ee5\u5e2e\u52a9\u60a8\u66f4\u8f7b\u677e\u5730\u6784\u5efaWeb\u5e94\u7528\u7a0b\u5e8f\u3002\u5982\u679c\u60a8\u662f\u4e00\u4e2a\u65b0\u624b\uff0c\u5efa\u8bae\u60a8\u4eceNode.js\u57fa\u7840\u77e5\u8bc6\u5f00\u59cb\uff0c\u5e76\u9010\u6b65\u5b66\u4e60\u66f4\u9ad8\u7ea7\u7684\u4e3b\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

Ubuntu\u662f\u4e00\u79cd\u57fa\u4e8eDebian\u7684Linux\u64cd\u4f5c\u7cfb\u7edf\uff0c\u662f\u5f00\u6e90\u8f6f\u4ef6\u7684\u4ee3\u8868\u4e4b\u4e00\u3002Node.js\u662f\u4e00\u4e2a\u57fa\u4e8eChrome V8\u5f15\u64ce\u7684JavaScript\u8fd0\u884c\u65f6\u73af\u5883\uff0c\u7528\u4e8e\u6784\u5efa\u5feb\u901f\u3001\u53ef\u6269\u5c55\u7684\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u3002\u672c\u6587<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[18207,1032,7748,116,18208],"topic":[],"class_list":["post-13416","post","type-post","status-publish","format-standard","hentry","category-zhuomianruanjian","tag-web","tag-1032","tag-7748","tag-116","tag-exe"],"_links":{"self":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/13416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/comments?post=13416"}],"version-history":[{"count":1,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/13416\/revisions"}],"predecessor-version":[{"id":13481,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/posts\/13416\/revisions\/13481"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/media?parent=13416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/categories?post=13416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/tags?post=13416"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.zhidianwl.net\/zhidianwl\/wp-json\/wp\/v2\/topic?post=13416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}