要安裝特定的nodejs版本,請參考使用NVM安裝特定的Nodejs版本。
步驟1:添加node.js ppa
node.js包在LTS版本和當(dāng)前版本中可用。可以根據(jù)需要選擇要在系統(tǒng)上安裝的版本。讓我們將ppa添加到系統(tǒng)中,以便在Ubuntu上安裝nodejs。
使用當(dāng)前版本:
$ sudo apt-get install curl python-software-properties $ curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -
使用LTS版本:
$ sudo apt-get install curl python-software-properties $ curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
對于該示例使用的是最新的當(dāng)前版本,并將它們的ppa添加到我的系統(tǒng)中。
步驟2:在Ubuntu上安裝node.js
可以成功地將node.js ppa添加到Ubuntu系統(tǒng)。現(xiàn)在使用apt get執(zhí)行下面的命令install node on和ubuntu。這也將安裝帶有node.js的NPM。此命令還將在系統(tǒng)上安裝許多其他依賴包。
$ sudo apt-get install nodejs
步驟3:檢查node.js和npm版本
安裝node.js之后,請驗(yàn)證并檢查已安裝的版本。可以在node.js官方網(wǎng)站上找到有關(guān)當(dāng)前版本的更多詳細(xì)信息。
$ node -v v11.12.0
另外,檢查NPM版本
$ npm -v 6.7.0
步驟4:創(chuàng)建演示W(wǎng)eb服務(wù)器(可選)
這是一個可選步驟。如果要測試node.js安裝。讓我們用“你好,世界!“文本”。創(chuàng)建文件server.js
$ vim server.js
并添加以下內(nèi)容
server.js
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(3000, "127.0.0.1"); console.log('Server running at http://127.0.0.1:3000/');
現(xiàn)在使用命令啟動node應(yīng)用程序。
$ node server.js debugger listening on port 5858 Server running at http://127.0.0.1:3000/
還可以使用以下命令啟用調(diào)試來啟動應(yīng)用程序。
$ node --inspect server.js Debugger listening on ws://127.0.0.1:9229/8976a32b-cf99-457c-85fb-e7288cbadac6 For help see https://nodejs.org/en/docs/inspector Server running at http://127.0.0.1:3000/
Web服務(wù)器已在端口3000上啟動。現(xiàn)在在瀏覽器中訪問http://127.0.0.1:3000/url。現(xiàn)在,只需要為應(yīng)用程序配置前端服務(wù)器。
本篇文章到這里就已經(jīng)全部結(jié)束了,更多其他精彩內(nèi)容可以關(guān)注PHP中文網(wǎng)的node.js視頻教程欄目!!!
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com