111 lines
3.5 KiB
JavaScript
111 lines
3.5 KiB
JavaScript
const express = require('express');
|
|
const { initDb, getAllArticles, getArticle, createArticle, updateArticle, deleteArticle, searchArticles } = require('./db');
|
|
const app = express();
|
|
const cors = require('cors');
|
|
const PORT = 9000;
|
|
|
|
app.use(cors())
|
|
app.use(express.json());
|
|
|
|
initDb().then(() => {
|
|
|
|
app.get('/', (req, res) => {
|
|
res.json({message : 'Server is running'});
|
|
});
|
|
|
|
app.get('/api/articles', (req, res) => {
|
|
try {
|
|
const articles = getAllArticles();
|
|
res.json(articles);
|
|
} catch (error) {
|
|
console.error('Error fetching articles:', error);
|
|
res.status(500).json({error: 'Failed to fetch articles', 'details': String(error)});
|
|
}
|
|
});
|
|
|
|
app.get('/api/articles/:ka_number', (req, res) => {
|
|
try {
|
|
const article = getArticle(req.params.ka_number);
|
|
|
|
if (!article) {
|
|
return res.status(404).json({ error: 'Article not found' });
|
|
}
|
|
|
|
res.json(article);
|
|
} catch (error) {
|
|
console.error('Error fetching article:', error);
|
|
res.status(500).json({error: 'Failed to fetch article', 'details': String(error)});
|
|
}
|
|
});
|
|
|
|
app.post('/api/articles', (req, res) => {
|
|
try {
|
|
const { title, content } = req.body;
|
|
|
|
if (!title) {
|
|
return res.status(400).json({error: 'Title is required' });
|
|
}
|
|
|
|
const article = createArticle(title, content || '');
|
|
res.status(201).json(article);
|
|
|
|
} catch (error) {
|
|
console.error('Error creating article:', error);
|
|
res.status(500).json({error: 'Failed to create article', 'details': String(error)});
|
|
}
|
|
});
|
|
|
|
app.put('/api/articles/:ka_number', (req, res) => {
|
|
try {
|
|
const { title, content } = req.body;
|
|
|
|
if (!title) {
|
|
return res.status(400).json({error: 'Title is required' });
|
|
}
|
|
|
|
const article = updateArticle(req.params.ka_number, title, content);
|
|
|
|
if (!article) {
|
|
return res.status(404).json({error: 'Article not found'});
|
|
}
|
|
|
|
res.json(article);
|
|
} catch (error) {
|
|
console.error('Error updating article:', error);
|
|
return res.status(500).json({error: 'Error while updating article', 'details': String(error)});
|
|
}
|
|
});
|
|
|
|
app.delete('/api/articles/:ka_number', (req, res) => {
|
|
try {
|
|
deleteArticle(req.params.ka_number);
|
|
return res.status(200).json({'message': 'Successfully deleted article'});
|
|
|
|
} catch (error) {
|
|
console.error('Error when deleting article:', error);
|
|
return res.status(500).json({error: 'Failed to delete article', 'details': String(error)});
|
|
}
|
|
});
|
|
|
|
app.get('/api/search', (req, res) => {
|
|
try {
|
|
const query = req.query.q;
|
|
|
|
if (!query) {
|
|
return res.status(400).json({error: 'Search query is required'});
|
|
}
|
|
|
|
const results = searchArticles(query);
|
|
res.json(results);
|
|
} catch (error) {
|
|
console.error('Error when searching articles:', error);
|
|
return res.status(500).json({error: 'Failed to search articles', 'details': String(error)});
|
|
}
|
|
});
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`Server running on http://localhost:${PORT}`);
|
|
});
|
|
}).catch(err => {
|
|
console.error('Failed to initialize database:', err);
|
|
}); |