
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
Micro2 — Node.js based asynchronous micro-library (WIP)
async and awaitImportant: Right now Micro2 not ready for production application. For making micro2 a goal is to faster moduler application development. Make your application with moduler way.
npm install --save micro2
Create an index.js file and export a function that accepts the standard http.IncomingMessage and http.ServerResponse objects:
module.exports = (req, res) => {
res.end('Welcome to Micro2')
}
or
module.exports = () => 'Welcome to Micro2'
async & awaitMicro2 is built for usage with async/await. You can read more about async / await.
const sleep = require('then-sleep')
module.exports = async (req, res) => {
await sleep(500)
return 'Ready!'
}
send(res, statusCode, data = null)require('micro2').send.statusCode is a Number with the HTTP status code, and must always be supplied.data is supplied it is sent in the response. Different input types are processed appropriately, and Content-Type and Content-Length are automatically set.
string: data is written as-is.400 error is thrown.So far we have used return to send data to the client. return 'Hello World' is the equivalent of send(res, 200, 'Hello World').
const {send} = require('micro2')
module.exports = async (req, res) => {
const statusCode = 400
const data = { error: 'Custom error message' }
send(res, statusCode, data)
}
send(res, statusCode, data = null)require('micro').send.statusCode is a Number with the HTTP status code, and must always be supplied.data is supplied it is sent in the response. Different input types are processed appropriately, and Content-Type and Content-Length are automatically set.
Stream: data is piped as an octet-stream. Note: it is your responsibility to handle the error event in this case (usually, simply logging the error and aborting the response is enough).Buffer: data is written as an octet-stream.object: data is serialized as JSON.string: data is written as-is.400 error is thrown. See Error Handling.You can use Micro2 programmatically by requiring Micro2 directly:
const micro2 = require('micro2')
const sleep = require('then-sleep')
const server = micro2(async (req, res) => {
await sleep(500)
return 'Hello world'
})
server.listen(3000)
You can applied middleware with server.
server.use((req, res, next) => { // err, req, res
log.info('middleware 1')
next()
})
Micro2 uses pino for logging. You could enable logging conditionaly. process.env.NODE_ENV
const log = server.log({
enabled: true
})
default export.require('micro2').http.createServer that uses the provided function as the request handler.await. So it can be async.require('micro').sendError.error.statusCode.error.message as the body.console.error and during development (when NODE_ENV is set to 'development') also sent in responses.throw.require('micro').createError.statusCode.orig sets error.originalError which identifies the original error (if any).Micro2 allows you to write robust microservices. This is accomplished primarily by bringing sanity back to error handling and avoiding callback soup.
If an error is thrown and not caught by you, the response will automatically be 500. Important: Error stacks will be printed as error and during development mode (if the env variable NODE_ENV is 'development'), they will also be included in the responses.
If the Error object that's thrown contains a statusCode property, that's used as the HTTP code to be sent. Let's say you want to write a rate limiting module:
const rateLimit = require('my-rate-limit')
module.exports = async (req, res) => {
await rateLimit(req)
// ... your code
}
Alternatively you can create the Error object yourself
if (tooMany) {
const err = new Error('Rate limit exceeded')
err.statusCode = 429
throw err
}
The nice thing about this model is that the statusCode is merely a suggestion. The user can override it:
try {
await rateLimit(req)
} catch (err) {
if (429 == err.statusCode) {
// perhaps send 500 instead?
send(res, 500)
}
}
If the error is based on another error that Micro2 caught, like a JSON.parse exception, then originalError will point to it. If a generic error is caught, the status will be set to 500.
In order to set up your own error handling mechanism, you can use composition in your handler:
const {send} = require('micro2')
const handleErrors = fn => async (req, res) => {
try {
return await fn(req, res)
} catch (err) {
console.log(err.stack)
send(res, 500, 'My custom error!')
}
}
module.exports = handleErrors(async (req, res) => {
throw new Error('What happened here?')
})
TODO
As always, you can run the Jest tests using: npm test and ESLint lint using: npm run lint
FAQs
Node.js based asynchronous micro-library (WIP)
We found that micro2 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.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.