How to build NetEase Cloud Music based on nodejs technology on the mobile phone

PHPz
Release: 2023-04-26 09:48:07
Original
804 people have browsed it

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.

  1. Installing Termux

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.

  1. Install Node.js

In Termux, enter the following command to install Node.js:

pkg install nodejs
Copy after login
  1. Create working directory

In Termux, enter the following command to create a folder named "NetEaseCloudMusic" and enter the folder:

mkdir NetEaseCloudMusic && cd NetEaseCloudMusic
Copy after login
  1. Initialize npm package

In the working directory, enter the following command to initialize the npm package:

npm init -y
Copy after login

After the above command is executed, a package.json file will be created. This file contains the project description, dependencies and other related information. .

  1. Install dependency packages

Next, you need to install the required dependency packages. In Termux, enter the following command:

npm install express body-parser request
Copy after login

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.

  1. Writing server code

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);
});
Copy after login

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.

  1. Run the server

In Termux, enter the working directory and execute the following command to start the server:

node server.js
Copy after login

After the above command is executed, the server will be successful The ground started.

  1. Verification

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.

  1. Conclusion

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!