-
Notifications
You must be signed in to change notification settings - Fork 0
/
server4.js
53 lines (46 loc) · 1.17 KB
/
server4.js
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
44
45
46
47
48
49
50
51
52
53
/*
To use this code, you will need to have the mysql module installed.
*/
const http = require('http');
const fs = require('fs');
const mysql = require('mysql');
const hostname = '127.0.0.1';
const port = 3000;
// Set up the MySQL connection
const connection = mysql.createConnection({
host: 'localhost',
user: 'user',
password: 'password',
database: 'database'
});
// Connect to the MySQL server
connection.connect();
const server = http.createServer((req, res) => {
// Serve the HTML for the website
if (req.url === '/') {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
res.end(`
<!doctype html>
<html>
<head>
<title>My Website</title>
</head>
<body>
<div style="text-align: center">
<img src="/image.jpg" alt="My Image">
</div>
</body>
</html>
`);
}
// Serve the image file
if (req.url === '/image.jpg') {
res.statusCode = 200;
res.setHeader('Content-Type', 'image/jpeg');
fs.createReadStream('image.jpg').pipe(res);
}
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});