是的,Ubuntu可以运行Node.js。您可以通过以下步骤在Ubuntu上安装和运行Node.js:
sudo apt update
sudo apt install nodejs npm
或者,您可以使用NodeSource PPA来安装最新版本的Node.js:
curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs
node --version
npm --version
如果安装成功,您将看到Node.js和npm的版本号。
mkdir my_project
cd my_project
npm init -y
npm install express
app.js
的文件,并在其中编写Node.js代码:const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
node app.js
现在,您的Node.js应用程序已经在Ubuntu上运行了。您可以访问http://localhost:3000来查看结果。
此外,您还可以使用诸如PM2这样的进程管理器来管理和监控Node.js应用程序。要安装PM2,请运行:
sudo npm install pm2 -g
然后使用以下命令启动您的Node.js应用程序:
pm2 start app.js
通过以上步骤,您可以在Ubuntu上成功安装、运行和管理Node.js应用程序。