-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (53 loc) · 1.92 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
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const accountsRouter = require('./routes/accounts');
const postsRouter = require('./routes/posts');
const contactsRouter = require('./routes/contacts');
const path = require('path');
const jikanjs = require('@mateoaranda/jikanjs');
require('dotenv').config();
const app = express();
const PORT = process.env.PORT || 3500;
// Middleware
app.use(cors());
app.use(express.json());
app.get('/', (req, res) => {
res.send('Server is running');
});
// Accounts route
app.use('/accounts', accountsRouter);
// Posts route
app.use('/posts', postsRouter);
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
// Contacts route
app.use('/contacts', contactsRouter);
// Jikan Anime API
app.get('/anime/synopsis', async (req, res) => {
const animeTitle = req.query.title; // Get the title from query parameters
if (!animeTitle) {
return res.status(400).json({ error: 'Anime title is required.' });
}
try {
// Search for the anime
const searchResults = await jikanjs.search('anime', animeTitle, 1);
if (searchResults && searchResults.data && searchResults.data.length > 0) {
const synopsis = searchResults.data[0].synopsis;
return res.json({ title: animeTitle, synopsis });
} else {
return res.status(404).json({ error: `No anime found with the title "${animeTitle}".` });
}
} catch (error) {
console.error('Error fetching data:', error);
return res.status(500).json({ error: 'An error occurred while fetching data.' });
}
});
// Connect to MongoDB
mongoose
.connect(process.env.MONGO_URI)
.then(() => console.log('Connected to MongoDB Atlas'))
.catch((error) => console.error('MongoDB connection error:', error));
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});