-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
119 lines (93 loc) · 3.35 KB
/
index.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { createRequire } from "module";
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const require = createRequire(import.meta.url);
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Using import or export module
import express from "express";
import { MongoClient } from 'mongodb';
import bodyParser from 'body-parser';
import os from 'os';
import { publicIp, publicIpv4, publicIpv6 } from 'public-ip';
// // Using require
// const express = require("express");
// const { MongoClient } = require('mongodb');
// const bodyParser = require("body-parser");
// const os = require('os');
// For mongodb
const mongoUrl= 'mongodb://3.81.12.106:27017/'// Replace with mongodb server IP
const client = new MongoClient(mongoUrl);
const db = client.db('mydatabase'); // Name of your database
const collection = db.collection('mycollection'); // Name of your collection
// Database connection function
async function connectToMongoDB() {
try {
await client.connect();
console.log('Connected to MongoDB server');
} catch (error) {
console.error('Error connecting to MongoDB Server:', error);
}
}
connectToMongoDB();
// await client.close(); // Close the connection
const app = express();
const PORT = process.env.PORT || 5000;
// Home page
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
// Parse incoming requests with JSON payloads
app.use(express.json());
app.use(express.static('/')); // Serve static files from root directory also we can use 'public' directory
// Insert data to MongoDB server
app.post('/insertData', async (req, res) => {
const data = req.body;
try {
// Check for duplicates
const existingData = await collection.findOne({ email: data.email });
if (existingData) {
return res.send(' email already exists, user adding fail!!');
// return res.status(400).send(' email already exists');
}
// Insert the data
await collection.insertOne(data);
res.status(200).send(' added successfully');
// console.log('User added successfully....')
} catch (error) {
// console.error('Error inserting data:', error);
return res.status(500).send(' add Error');
}
});
// Get data from MongoDB server
app.get('/fetchData', async (req, res) => {
const data = await collection.find({}).limit(12).sort({ _id: -1 }).toArray()
res.json(data);
// console.log('User fetch successfully....')
});
// Find host and ip address
app.get('/hostinfo', async (req, res) => {
const hostname = os.hostname(); // Get the server's hostname
const networkInterfaces = os.networkInterfaces();
let privateIp = '';
// Find the private IP address
for (const iface in networkInterfaces) {
for (let i = 0; i < networkInterfaces[iface].length; i++) {
if (networkInterfaces[iface][i].family === 'IPv4' && !networkInterfaces[iface][i].internal) {
privateIp = networkInterfaces[iface][i].address;
break;
}
}
if (privateIp) break;
}
let publicIpAddress = await publicIpv4();
const hostinfo = {
hostname,
privateIp,
publicIpAddress
};
res.json(hostinfo);
});
app.listen(PORT, () => {
console.log('Server is running on', PORT);
});