Home  >  Article  >  PHP Framework  >  Building a Movie Website with Powerful Search: Webman’s Guide to Movie Applications

Building a Movie Website with Powerful Search: Webman’s Guide to Movie Applications

王林
王林Original
2023-08-12 11:25:521477browse

Building a Movie Website with Powerful Search: Webman’s Guide to Movie Applications

Building a movie website with powerful search functions: Webman’s Movie Application Guide

Introduction: With the popularity and development of the Internet, more and more people are accustomed to using it Movie website to browse and select movies. This article will introduce how to build a movie website with powerful search functions - Webman, and demonstrate its implementation process through code examples.

1. Project preparation

In order to build Webman, we first need to prepare the following tools and resources:

  1. Node.js: a JavaScript based on the Chrome V8 engine Runtime environment for back-end development.
  2. Express.js: A fast, open, minimalist web application framework for quickly building server-side applications.
  3. MongoDB: A non-relational database used to store movie information data.
  4. TMDb API: An API that provides movie information and movie database. We will use this API to get movie data.

2. Create the project

First, we create a folder named Webman on the computer, then open the command line tool, enter the folder, and execute the following command to Initialize a new Node.js project:

npm init -y

Then, we install Express.js and MongoDB’s Node.js driver:

npm install express mongodb

Next, we create a project called index.js file and import the required modules in it:

const express = require('express');
const MongoClient = require('mongodb').MongoClient;

const app = express();
const port = 3000;

// 在这里编写代码

3. Get movie data

We will use TMDb API to get movie data. First, we need to apply for an API key on the TMDb official website. After obtaining the API key, we can use the following code to send a request to TMDb and obtain movie data:

const apiKey = 'YOUR_API_KEY';

app.get('/movies', (req, res) => {
  const url = `https://api.themoviedb.org/3/discover/movie?api_key=${apiKey}`;

  // 使用Node.js的内置http模块来发送请求
  http.get(url, (response) => {
    let data = '';

    response.on('data', (chunk) => {
      data += chunk;
    });

    response.on('end', () => {
      const movies = JSON.parse(data).results;
      res.send(movies);
    });
  });
});

4. Store movie data

Next, we need to get the movie Data is stored into a MongoDB database. First, we need to create a database named webman in MongoDB and create a collection named movies. We can then use the following code to store the data into the database:

const url = 'mongodb://localhost:27017';
const dbName = 'webman';

MongoClient.connect(url, (err, client) => {
  if (err) {
    console.log(err);
  } else {
    console.log('Connected to MongoDB');

    const db = client.db(dbName);
    const collection = db.collection('movies');
    
    app.get('/movies/save', (req, res) => {
      const url = `https://api.themoviedb.org/3/discover/movie?api_key=${apiKey}`;

      http.get(url, (response) => {
        let data = '';

        response.on('data', (chunk) => {
          data += chunk;
        });

        response.on('end', () => {
          const movies = JSON.parse(data).results;
          
          collection.insertMany(movies, (err, result) => {
            if (err) {
              console.log(err);
              res.send(err);
            } else {
              console.log('Movies saved to database');
              res.send('Movies saved to database');
            }
          });
        });
      });
    });
  }
});

5. Implement the search function

Now, we will add the search function to Webman. We can use the following code to implement a simple search interface:

app.get('/movies/search', (req, res) => {
  const keyword = req.query.keyword;

  const query = {
    $or: [
      { title: { $regex: keyword, $options: 'i' } },
      { overview: { $regex: keyword, $options: 'i' } }
    ]
  };

  collection.find(query).toArray((err, result) => {
    if (err) {
      console.log(err);
      res.send(err);
    } else {
      res.send(result);
    }
  });
});

Now, we can implement the search function by calling /movies/search?keyword=keyword.

6. Test

At this point, the basic functions of Webman have been implemented. We can use the following command to start the server and visit http://localhost:3000/movies/search?keyword=Wolverine in the browser to test the search function:

node index.js

If Everything works fine and you will see the movie data returned.

Conclusion: This article introduces how to build a movie website with powerful search function - Webman. By using Node.js, Express.js and MongoDB, we successfully implemented the acquisition, storage and search functions of movie data. I hope this article helps you build your own movie website.

The above is the detailed content of Building a Movie Website with Powerful Search: Webman’s Guide to Movie Applications. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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