Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
@dellasera/plugdo-mvc
Advanced tools
A module that implements mvc
, webapi
and realtime
web software architecture. It help you focus in the implementation of the business logic, instance of writing express routing each time you need a new section.
Compatibility: Node.js version 6+ and Express version 4+
Install with npm
:
npm install @dellasera/plugdo-mvc
In order to found the components, a directory structure must be created as follow:
--content
- source
--source/controllers
--source/dependencies
--source/webapi
--source/realtime
--views
Directories definitions:
content: you will add the css, js, font files.
source: the MVC logic will be added here.
source/controllers: You can create directories and js files, the js files will have the controller logic.
source/dependencies: You can create directories and js files, the js files will have the services, utilities, classes, helper, etc.
source/webapi: You can create directories and js files, the js files will have the web api logic.
source/realtime: You can create directories and js files, the js files will have the realtime channel logic.
views: the EJS views will be added here.
The error and 404 not found are returned in XML format, because the PLUGDO MVC is a mix between Web Page and Web API, the general response for erros and 404 not found was changed to XML format.
Create a new controller is easy, just add a new file in source/controllers, plugdo MVC will load the new file after initialization, the loading is not automatic. You must restart the nodejs execution.
First, start the plugdo-mvc server. (use nodemon for development is recommended)
const mvc = require("@dellasera/plugdo-mvc").mvc();
const path = require("path");
const port = process.env.PORT === undefined ? 3000 : process.env.PORT;
mvc.start(port, path.resolve(__dirname)); // The port and root path is required
Create a controller, adding a file in source/controllers. The controller must return a json model. An example is:
mvc.controller({
name: "customer",
action: "index"
}, function (req) {
return {
title: "Customer Home Page",
message: "Welcome to our web page!"
};
});
The expected URL will be:
http://domain.com/customer/index
You can also replace the default URL format as name/action, just including the path: "/new-path". Also a dependency injection simple way is available to pass the dependencies previously defined.
mvc.controller({
name: "customer",
action: "index",
path: "/new-path"
}, "customerService", function (req, customerService) {
return customerService.getAll();
});
The expected URL will be:
http://domain.com/new-path
The default view to be required is:
views/customer/index.ejs
If you need to use another file, skiping the default, just add the view option as follow:
mvc.controller({
name: "customer",
action: "index",
view: "customer-index.ejs" // Also a directory, view: "customer/index/home.ejs"
}, function (req) {
return {
title: "Customer Home Page",
message: "Welcome to our web page!"
};
});
Create a new dependency is easy, just add a new file in source/dependencies, plugdo MVC will load the new file after initialization, the loading is not automatic. You must restart the nodejs execution.
mvc.dependencies.customerService = {
"getAll": function () {
return [
{
name: "Marco A. Castillo",
country: "Panama"
},
{
name: "Rafael Della Sera",
country: "Panama"
}
]
}
};
Added in version 1.0.31 the mvc.service() fnction. This new function allow you to add services or collect services. The same example above:
mvc.service("customerService", {
"getAll": function () {
return [
{
name: "Marco A. Castillo",
country: "Panama"
},
{
name: "Rafael Della Sera",
country: "Panama"
}
]
}
});
Create a new webapi is easy, just add a new file in source/webapi, plugdo MVC will load the new file after initialization, the loading is not automatic. You must restart the nodejs execution.
mvc.api({
name: "customer",
action: "all"
}, function (req) {
return [
{
name: "Marco A. Castillo",
country: "Panama"
},
{
name: "Rafael Della Sera",
country: "Panama"
}
];
});
The expected URL for XML format will be:
http://domain.com/api/customer/all/xml
The expected URL for JSON format will be:
http://domain.com/api/customer/all/json
You can also replace the default URL format as name/action, just including the path: "/new-path". Also a dependency injection simple way is available to pass the dependencies previously defined.
mvc.api({
name: "customer",
action: "all",
path: "/service/customer/get/all"
}, "customerService", function (req, customerService) {
return customerService.getAll();
});
The expected URL for XML format will be:
http://domain.com/api/service/customer/get/all/xml
The expected URL for JSON format will be:
http://domain.com/api/service/customer/get/all/json
Create a new realtime channel is easy, just add a new file in source/realtime, plugdo MVC will load the new file after initialization, the loading is not automatic. You must restart the nodejs execution.
mvc.realtime("channel1");
Create a new ejs view file, including the required socket.io files and publish the new view creating a Controller.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Realtime Example</title>
<link rel="stylesheet" href="/css/enterpriseui.min.css"/>
<script src="/socket.io/socket.io.js"></script>
<script src="/js/plugdo-dependency.min.js"></script>
<script src="/js/plugdoeui.min.js"></script>
</head>
<body id="eui">
<script>
var realtime = new PlugdoRealtime("http://localhost:3000");
function PlugdoRealtime (host) {
var self = this;
self.io = io.connect(host);
this.register = function (name, callback) {
self.io.on("client" + name, function (data) {
callback(data);
});
};
this.send = function (name, data) {
self.io.emit("server" + name, data);
}
}
load(function () {
plugdo.ready(function() {
realtime.send("channel1", { date: Date.now(), author: "Marco Castillo"} );
realtime.register("channel1", function (data) {
console.log(data);
});
});
}, true);
</script>
</body>
</html>
Create a new Controller or Web API.
mvc.broadcast.publish("channel1", { date: Date.now(), author: "Marco Castillo"});
mvc.api({
name: "customer",
action: "new"
}, "customerService", function (req, customerService) {
// Create the customer model
let customer = {
date: Date.now(),
author: req.params.name
};
// Insert the new customer and collect the response back from the service
let responseModel = customerService.createCustomer(customer);
if(responseModel.success) {
// Send the new customer to the realtime channel if the customer was created successfully
mvc.broadcast.publish("channel1", customer);
}
return responseModel;
});
mvc.upload("/upload");
Adding this path using the method mvc.upload before the mvc.start is executed, allow you to accept files through form multipart. the default behavior is to response a json object. The /upload path become a Web API. If you want to avoid this default behavior, you must pass options to the mvc.upload.
The following next=true options, will upload the file and create a json model injected in the req.files. The mvc.upload accept multiple files by default so the req.files is an array. With the next=true option, you can create a controller or a web api. Now you can execute your on logic, like, uploading a file and adding the file in the database.
mvc.upload("/upload", { next: true });
You can register to the realtime channel "plugdo-mvc-upload-in-progress" to collect the progress of the file. The model sent is:
let realtimeModel = {
path,
file: {
field,
name,
path,
encoding,
type,
size
}
}
Create a new micro-service is easy, just implement the ms.start() framework to start working with RabbitMQ task. You can create an application that just open a connection with RabbitMQ to consume or emit message to the queue.
Let's see the following example to open a connection with RabbitMQ waiting for a payload message in JSON format. (Just JSON payload is supported).
// index.js
const ms = require("./modules/plugdo-mvc").ms();
// "localhost" is the host used in this example
ms.start("localhost").then(() => {
// Queue channel "PLUGDO_MS"
// ms.consume() function register this queue as listener
ms.consume("PLUGDO_MS", function (payload, end) {
// Add your own logic here to process the payload JSON object
end(payload); // Always end the process
});
});
Also you can emit message. This node app will end as soon as the message is sent. The connection remain open when you consume a queue, otherwise the app will close.
// index.js
const ms = require("./modules/plugdo-mvc").ms();
// "localhost" is the host used in this example
ms.start("localhost").then(() => {
// Queue channel "PLUGDO_MS"
// Emit a message to this queue
let payload = {
id: 1,
message: "Payload Message"
};
ms.emit("PLUGDO_MS", payload);
});
The following demo consume a queue, and also emit a message to that queue. You will be able to log the payload recieved in the consume method.
// index.js
const ms = require("./modules/plugdo-mvc").ms();
// "localhost" is the host used in this example
ms.start("localhost").then(() => {
// Queue channel "PLUGDO_MS"
// ms.consume() function register this queue as listener
ms.consume("PLUGDO_MS", function (payload, end) {
// Add your own logic here to process the payload JSON object
console.log(payload);
end(payload); // Always end the process
}).emit("PLUGDO_MS", {
id: 1,
message: "Payload Message"
});
});
The req parameter injected in the controller is the express object, if you need more details about the properties available, just go to expressjs.com documentation.
FAQs
plugdo mvc web server
We found that @dellasera/plugdo-mvc demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.