单体应用的模块化开发代码例子
时间: 2024-04-14 15:09:35 浏览: 18
以下是一个使用模块化开发的单体应用的代码例子:
```
// app.js
// 引入模块
const express = require('express');
const bodyParser = require('body-parser');
const userRoutes = require('./routes/userRoutes');
const postRoutes = require('./routes/postRoutes');
// 初始化应用
const app = express();
// 中间件
app.use(bodyParser.json());
// 路由
app.use('/users', userRoutes);
app.use('/posts', postRoutes);
// 启动应用
app.listen(3000, () => {
console.log('应用已启动');
});
```
```
// routes/userRoutes.js
const express = require('express');
const userController = require('../controllers/userController');
const router = express.Router();
router.get('/', userController.getAllUsers);
router.get('/:id', userController.getUserById);
router.post('/', userController.createUser);
router.put('/:id', userController.updateUser);
router.delete('/:id', userController.deleteUser);
module.exports = router;
```
```
// controllers/userController.js
const users = [];
function getAllUsers(req, res) {
res.json(users);
}
function getUserById(req, res) {
const userId = req.params.id;
const user = users.find(u => u.id === userId);
if (user) {
res.json(user);
} else {
res.status(404).json({ message: '用户不存在' });
}
}
function createUser(req, res) {
const { name, age } = req.body;
const newUser = { id: Date.now().toString(), name, age };
users.push(newUser);
res.status(201).json(newUser);
}
function updateUser(req, res) {
const userId = req.params.id;
const { name, age } = req.body;
const userIndex = users.findIndex(u => u.id === userId);
if (userIndex !== -1) {
users[userIndex] = { ...users[userIndex], name, age };
res.json(users[userIndex]);
} else {
res.status(404).json({ message: '用户不存在' });
}
}
function deleteUser(req, res) {
const userId = req.params.id;
const userIndex = users.findIndex(u => u.id === userId);
if (userIndex !== -1) {
users.splice(userIndex, 1);
res.json({ message: '删除成功' });
} else {
res.status(404).json({ message: '用户不存在' });
}
}
module.exports = {
getAllUsers,
getUserById,
createUser,
updateUser,
deleteUser,
};
```
```
// routes/postRoutes.js
const express = require('express');
const postController = require('../controllers/postController');
const router = express.Router();
router.get('/', postController.getAllPosts);
router.get('/:id', postController.getPostById);
router.post('/', postController.createPost);
router.put('/:id', postController.updatePost);
router.delete('/:id', postController.deletePost);
module.exports = router;
```
```
// controllers/postController.js
const posts = [];
function getAllPosts(req, res) {
res.json(posts);
}
function getPostById(req, res) {
const postId = req.params.id;
const post = posts.find(p => p.id === postId);
if (post) {
res.json(post);
} else {
res.status(404).json({ message: '文章不存在' });
}
}
function createPost(req, res) {
const { title, content } = req.body;
const newPost = { id: Date.now().toString(), title, content };
posts.push(newPost);
res.status(201).json(newPost);
}
function updatePost(req, res) {
const postId = req.params.id;
const { title, content } = req.body;
const postIndex = posts.findIndex(p => p.id === postId);
if (postIndex !== -1) {
posts[postIndex] = { ...posts[postIndex], title, content };
res.json(posts[postIndex]);
} else {
res.status(404).json({ message: '文章不存在' });
}
}
function deletePost(req, res) {
const postId = req.params.id;
const postIndex = posts.findIndex(p => p.id === postId);
if (postIndex !== -1) {
posts.splice(postIndex, 1);
res.json({ message: '删除成功' });
} else {
res.status(404).json({ message: '文章不存在' });
}
}
module.exports = {
getAllPosts,
getPostById,
createPost,
updatePost,
deletePost,
};
```
在以上例子中,我们将应用拆分成了多个模块,每个模块都有自己的路由和控制器,使得代码更加清晰和易于维护。通过在 app.js 中引入模块,我们可以将模块组合起来形成一个完整的应用。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)