MedusaJS: Building Server-side Applications
Janet Awino
Posted on March 13, 2023
MedusaJS is a popular backend JavaScript framework that allows developers to build scalable web applications. It is built on top of Node.js and uses the Express.js web framework to handle requests and responses. Its key features include easy integration with other libraries, a built-in ORM, and support for MVC architecture.
With MedusaJS, developers can focus on writing business logic and implementing features rather than dealing with the boilerplate code required to set up a server-side application. The framework uses ExpressJS under the hood, making it easy to get started with and familiar for developers who have worked with Express before.
Here's a sample code that demonstrates how to build a server-side application using MedusaJS:
const Medusa = require('medusa-js');
const bodyParser = require('body-parser');
const cors = require('cors');
const morgan = require('morgan');
const { User } = require('./models');
const app = new Medusa();
// Middleware
app.use(bodyParser.json());
app.use(cors());
app.use(morgan('dev'));
// Routes
// GET all users
app.get('/users', async (req, res) => {
const users = await User.findAll();
res.json(users);
});
// GET user by ID
app.get('/users/:id', async (req, res) => {
const user = await User.findByPk(req.params.id);
if (user) {
res.json(user);
} else {
res.status(404).json({ error: 'User not found' });
}
});
// POST new user
app.post('/users', async (req, res) => {
const user = await User.create(req.body);
res.json(user);
});
// PUT update user
app.put('/users/:id', async (req, res) => {
const user = await User.findByPk(req.params.id);
if (user) {
await user.update(req.body);
res.json(user);
} else {
res.status(404).json({ error: 'User not found' });
}
});
// DELETE user
app.delete('/users/:id', async (req, res) => {
const user = await User.findByPk(req.params.id);
if (user) {
await user.destroy();
res.sendStatus(204);
} else {
res.status(404).json({ error: 'User not found' });
}
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ error: 'Internal server error' });
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server started on port ${PORT}`);
});
In this code, we start by importing the necessary dependencies and our User
model. We then create a new instance of the Medusa
class and define our middleware stack, which includes the body-parser
, cors
, and morgan
middleware.
Next, we define our routes for CRUD operations on users. We use the async/await
syntax to await the results of queries to the database through SequelizeJS, as these operations are asynchronous.
Finally, we define an error-handling middleware function that logs errors to the console and sends a 500 Internal Server Error response to the client when an error occurs. We then start the server listening on the appropriate port, which is either specified through the PORT
environment variable or defaults to port 3000.
In conclusion, this code shows how to build a fully-functional server-side application using MedusaJS, with support for creating, reading, updating, and deleting user records from a PostgreSQL database. If you are looking for a lightweight and flexible way to build server-side applications with JavaScript, MedusaJS is definitely worth checking out.
Posted on March 13, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.