-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
43 lines (36 loc) · 977 Bytes
/
index.js
File metadata and controls
43 lines (36 loc) · 977 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const express = require('express');
const mysql = require('mysql');
const app = express();
// Create a MySQL connection
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'movie-db'
});
// Connect to the MySQL server
connection.connect((err) => {
if (err) {
console.error('Error connecting to MySQL:', err);
return;
}
console.log('Connected to MySQL server');
});
// Define a route to get all movies
app.get('/movies', (req, res) => {
const query = 'SELECT * FROM all_movies';
// Execute the query
connection.query(query, (err, results) => {
if (err) {
console.error('Error executing query:', err);
res.status(500).send(err);
return;
}
// Send the movies as a response
res.json(results);
});
});
// Start the server
app.listen(3000, () => {
console.log('Server started on port 3000');
});