Linux is a free software and open source Unix operating system, while Node.js is a platform for writing server-side applications using JavaScript. They are both very popular technologies in web development, and how to use Node.js on Linux has become an important issue for web developers. In this article, we will take an in-depth look at how to use Node.js on Linux to build various applications.
First, we need to install Node.js on Linux. There are multiple ways to install Node.js, but we’re using the most common method here: installing via the command line. The following are simple steps to install Node.js on Linux:
(1) Open a terminal and enter the following command:
sudo apt-get update
This command will update your Linux package manager to ensure the latest version software is available.
(2) Next, you need to run the following command:
sudo apt-get install nodejs
This command will use the Linux package manager to install Node.js.
(3) Then run the following command:
sudo apt-get install npm
This command will install the Node.js package manager npm.
Now that we have installed Node.js on Linux, next we will create a simple web application using Node.js Web application.
(1) Open the terminal and switch to the directory where you want to save the code and create a server.js
file:
cd /path/to/your/project touch server.js
(2) Use your favorite Open the server.js
file in the editor and enter the following content:
const http = require('http'); const hostname = '127.0.0.1'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World '); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
This code will create an HTTP server and access http://127.0.0.1:3000
Send a "Hello World" message to the client.
(3) Run the following command:
node server.js
This command will start the server and you will see a "Server running at http://127.0.0.1:3000/" message.
(4) Now, open the browser and go to http://127.0.0.1:3000/
, you will see the browser displays "Hello World".
Now we have seen how to create a web application using Node.js on Linux. Next, we will introduce how to connect to the database using Node.js.
(1) First, we need to install a popular Node.js client for connecting to the MySQL database. Run the following command in the terminal:
npm install mysql
(2) Log in to the MySQL server using the format mysql -u account name -p
.
(3) Create a database table:
CREATE TABLE users ( id INT(11) NOT NULL AUTO_INCREMENT, username VARCHAR(50) NOT NULL, email VARCHAR(50) NOT NULL, password VARCHAR(50) NOT NULL, PRIMARY KEY (id) );
(4) Use the following code to connect the Node.js application to the MySQL database:
const mysql = require('mysql'); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'mydatabase' }); connection.connect((err) => { if (err) { console.error('error connecting: ' + err.stack); return; } console.log('connected as id ' + connection.threadId); });
This code will connect to MySQL database and print out the ID of the connection thread.
(5) Then you can use the following code to insert data into the database table:
const sql = "INSERT INTO users (username, email, password) VALUES ('john', 'john@gmail.com', 'password')"; connection.query(sql, (err, result) => { if (err) throw err; console.log("1 record inserted"); });
This code will insert a new record into the users
table.
Overall, Node.js and the Linux platform are important parts of web development. In this article, we covered how to install Node.js on Linux, create a web application, and connect to a database using Node.js. Whether you are a newbie or an experienced developer, these techniques will be helpful for you in building a variety of applications. The power and flexibility of Node.js and Linux make them the first choice for web developers, and more and more developers are joining this community.
The above is the detailed content of How to play Linux nodejs. For more information, please follow other related articles on the PHP Chinese website!