Home >Web Front-end >Front-end Q&A >How to build NetEase Cloud Music based on nodejs technology on the mobile phone
With the continuous development of Internet technology, more and more companies are beginning to use nodejs to build back-end services. In this article, I will share how to build NetEase Cloud Music based on nodejs technology on the mobile phone.
Termux is a terminal emulator powerful enough that allows you to run the Linux command line in the Android system and install Node.js , MySQL and Python and other environments. Open Google Play to download Termux, or you can go to the official website to download it directly.
In Termux, enter the following command to install Node.js:
pkg install nodejs
In Termux, enter the following command to create a folder named "NetEaseCloudMusic" and enter the folder:
mkdir NetEaseCloudMusic && cd NetEaseCloudMusic
In the working directory, enter the following command to initialize the npm package:
npm init -y
After the above command is executed, a package.json file will be created. This file contains the project description, dependencies and other related information. .
Next, you need to install the required dependency packages. In Termux, enter the following command:
npm install express body-parser request
After the above command is executed , the running environment of the Express framework will be installed, the body-parser module is used to parse the request body, and the request module is used to process HTTP requests.
Create a file named "server.js" in the working directory and write as follows:
const express = require('express'); const bodyParser = require('body-parser'); const request = require('request'); const app = express(); const port = 3000; app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); // 静态文件服务目录 app.use(express.static('public')); // 获取歌曲列表 app.get('/songList', function(req, res) { const url = 'https://api.imjad.cn/cloudmusic/?type=playlist&id=3778678'; request(url, function(error, response, body) { if (!error && response.statusCode == 200) { const songList = JSON.parse(body); res.send(songList); } }) }); // 监听端口 app.listen(port, function() { console.log('Server running on port ' + port); });
Above In the code, an express application is created and the body-parser middleware is used to parse the request Body. Created a static file serving directory so that static files can be rendered on the server. Created a route with the URL "/songList", obtained the song list through the API and returned the response to the client. Finally, let the application listen to client requests on port 3000 through the app.listen() method.
In Termux, enter the working directory and execute the following command to start the server:
node server.js
After the above command is executed, the server will be successful The ground started.
Now open the browser on your phone and enter "localhost:3000". A static page will be displayed on the web page. The page will be displayed in the specified in the public directory. Enter "localhost:3000/songList" in the URL, and you can see that the song list of NetEase Cloud Music has been successfully obtained.
In this article, I shared how to build NetEase Cloud Music based on nodejs technology on a mobile phone. Through this article, you can also build nodejs-based web applications in your own device. Looking forward to your practice and sharing your experience.
The above is the detailed content of How to build NetEase Cloud Music based on nodejs technology on the mobile phone. For more information, please follow other related articles on the PHP Chinese website!