
Security News
npm ‘is’ Package Hijacked in Expanding Supply Chain Attack
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
**MVCraft** is back-end API framework based on MVC *(Model-Controller-View)* architecture model. It provides many built-in features, such as: parsers (cookies, body etc). It has been created to allow developers to create APIs much faster with less code an
MVCraft is back-end API framework based on MVC (Model-Controller-View) architecture model. It provides many built-in features, such as: parsers (cookies, body etc). It has been created to allow developers to create APIs much faster with less code and modules!
npm install mvcraft@latest
import { App } from 'mvcraft'
const config = {
port: process.env.PORT,
maxBodySize: 1024,
connectionTimeout: 10000 // Send timeout handler after 10s if no response
}
const Server = App(config)
...
Server.Run(port => console.log(`Server is running on ${port} port`))
// Server is running on 3000 port by default if not provided in config
Controller is basically function that handles an endpoint. However it has many more features described later that allows easy scaling.
import { App, Controller } from 'mvcraft'
const Server = App()
const GreetingController = Controller('/greet', 'GET', (methods) => {
const { SetHeader, Response } = methods
SetHeader('Content-Type', 'text/plain')
Response(200, 'Hello!')
})
Server.AddController(GreetingController.Build())
Server.Run(callback)
Methods is object argument that provides you all necessary utilities, such as Response for sending data, Content for gathering data (such as query, parameters, path, body etc).
For more complex endpoints, such as /sign-in where responses may be different, based on authorization etc you can define view.
import { App, Controller, View } from 'mvcraft'
const Server = App()
const GreetingView = View('GreetingView', (name) => {
return {
success: true,
message: `Hello there, ${name}!`
}
})
const GreetingController = Controller('/greet', 'GET', (methods) => {
const { Content, SetHeader, Response } = methods
SetHeader('Content-Type', 'application/json')
const { name } = Content().query.name || 'John doe'
/* Content() returns object containing:
-query params
-path params
-headers (sent and received)
-cookies
-path
-method
-body
*/
Response(200, 'GreetingView', name)
/* In this case GreetingView will be replaced by its view output and name is passed as third argument to view handler */
})
Server.AddView(GreetingView)
Server.AddController(GreetingController.Build())
Server.Run(callback)
Also MVCraft is automatically converting data to JSON if you provide content-type application/json header and if you receive body with content-type application/json header it also parses it automatically for you!
If you have some functions that are used very often and you don't want to import it all over and over you can create services. They will be available in every controller and endpoints!
import { App, Controller, Service } from 'mvcraft'
const Server = App()
const UserView = ('UserView', (user) => {
if(!user) return { success: false, message: 'User not found' }
return { success: true, data: user }
})
const UserController = ('/user/:id', 'GET', (methods) => {
const { Content, SetHeader, Response, Execute } = methods
SetHeader('Content-Type', 'application/json')
const { id } = Content().params.id
const user = await Execute('GetUser', id)
// You access service by its name and you can provide parameters to it
if(!user) return Response(404, 'UserView')
Response(200, 'UserView', user)
})
const GetUserService = ('GetUser', async (id) => {
const user = await // your code logic goes here...
return user
})
Server.AddService(GetUserService)
Server.AddView(UserView)
Server.AddController(UserController.Build())
Server.Run(callback)
If you want to scale your project you can use controller not only as a single route handler but as a "container" for multiple endpoints!
...
const UserController = ('/user/:endpoint', '*', ({ Endpoint, End }) =>
Endpoint('sign-in', 'POST', ({ Response }) => {
...
Response(200, 'Signed in!')
})
Endpoint('sign-up', 'POST', ({ Response }) => {
...
Response(200, 'Signed up!')
})
Endpoint('info', 'GET', ({ Response }) => {
...
Response(200, 'UserView', userData)
})
End()
//IMPORTANT! You must provide End() after all endpoints declaration
})
...
MVCraft provides built-in error handling system that prevents your app from crashing. It sends responses for scenarios such as: path not found, body payload is too large or any other unhandled error. You can modify it and set your custom responses easily.
...
const UserController = ('/user/:endpoint', '*', (methods) => {
// Here is some unhandled error example
throw new Error('custom_error')
})
UserController.Handle(('custom_error', ({ Response }, err) => {
Response(500, ...)
})
// You can also provide "default" handler for any uncaught errors
UserController.Handle('*', ({ Response }, err) => {
console.log(err)
Response(500, ...)
})
...
This Handle function will also get errors from services or endpoints used in UserController which means you don't have to write all the time try / catch in your code!
FAQs
**MVCraft** is back-end API framework based on MVC *(Model-Controller-View)* architecture model. It provides many built-in features, such as: parsers (cookies, body etc). It has been created to allow developers to create APIs much faster with less code an
The npm package mvcraft receives a total of 6 weekly downloads. As such, mvcraft popularity was classified as not popular.
We found that mvcraft demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
Security News
A critical flaw in the popular npm form-data package could allow HTTP parameter pollution, affecting millions of projects until patched versions are adopted.
Security News
Bun 1.2.19 introduces isolated installs for smoother monorepo workflows, along with performance boosts, new tooling, and key compatibility fixes.