loading...
Cover image for Grandjs New Features

Grandjs New Features

tareksalem profile image tareksalem ・4 min read

If you like building new things and collaborate on new projects, please don't hesitate to join Grandjs community on GitHub by contributing us and starring the project to gain more popularity.
I talked here several times about a promising nodejs framework is coming to the web development track, this framework is called grandjs, it's super easy, fast to start and implement and extendable.

One of the most important things in Grandjs is permanent development and implementing new features, so I am here today to introduce to you the new features in grandjs.

Grandjs Now implements uploading files

When I started nodejs development it was about for years ago, I remember the first way I wrote a javascript code to build a website that has the ability to upload files, I used multer package at this time, it was amazing and easy to use but when I learned nodejs I realized that am a developer who needs more than flexible package to upload files to system, check these files and save them if I want instead of uploading junk files and then delete the image if it doesn't match the validation as what was happening in multer, then express-fileupload package appeared and it was more suitable for me but I still wanted more flexibility, here is the flexibility that Grandjs came with, it's super easy to upload files, access them and save these files in different ways, let's see the following example:

postAdminPage() {
    return {
        method: "post",
        url: "/admin/uploadInfo",
        handler: (req, res) => {
            console.log(req.files) //output {} contains all uploaded files
        }
    }
}

it's just a property you call from request object, all this stuff and the file is not saved yet on our server which makes your website more secure and clean from junk files, req.files is an object contains the uploaded files every file is a property with the sent field name, it contains many properties allows you see all information about that file easily

{
      name: 'images' // the name of the filed,
      filename: 'download (1).jpeg' // original file name,
      data: <Buffer> // the file content as buffer,
      size: 14473 // size of file,
      extension: '.jpg' // the extension type of file,
      mimetype: 'image/jpeg' // mime type of file,
      stream: [Duplex] // ability to use this file to stream it,
       headers: [Object] // the sent headers with this file,
      byteOffset: 147,
      byteCount: 27975 // bytes count
}

if you can see, you have the name of the field, filename which is the original filename, data which is the buffer of the file to use it to write the file on the server, size property which incarnates the file size, mimetype which is the type of file to check easily on it and extension of the file to make any condition you want on the uploaded file.

One of the most amazing things in this file upload is the ability to use the stream property of the uploaded file to pipe it to any writable stream such as writing it in a file in the system or pipe it to a response or doing anything you want.

Global Middleware in Grandjs

here is the second major feature added in grandjs v 1.3.0. The ability to add global middlewares to the whole application before executing any routes, it helps you to add some settings, make conditions on the requests, enable cors, or injecting dependencies inside the app.

Grandjs.use((req, res, next) => {
    // continue
    return next();
});

Grandjs.use is a function that takes one argument which is a function that has three parameters request, response, next. next here is a function can be executed to allow the application to go to the next middleware or executing the matched router.

Grandjs Validation Library

we created a newly separated package under the name of grandjs, this package is responsible for validation works, it's still under updates and enhancements but one of the amazing features in this package is the ability to create a schema for your incoming data!

maybe you now ask yourself why creating schema however I have mongoose schema !!
yes, you are right, but I think there is a misunderstanding.
When you build a new application based on nodejs, you start choosing the packages that you will use in the app, and one of these packages surely is a validation package which will be used in middlewares to validate the incoming data before working on these data and passing them to the database and that is exactly what grand-validator does.

Grand-validator trying to validate your data in different ways, the published way until that is creating a schema and model for the data and then call validate method to validate the incoming data against the schema, this step returns to you an array of the smart validation objects contain a message, a field name that has the error, the type value should be and the current type of the wrong field.

Example

var userSchema = new Schema({
    name: {
        type: String
    },
    email: {
        type: String
    },
    age: {
        type: Number
    }
})
// validate the schema
userSchema.validate({
    name: "tarek",
    email: "tarek@gmail.com",
    age: 20
})

These were the major features added to Grandjs version 1.3.0, please don't hesitate to support us on github and npm, also don't forget to try it and give us the feedback, thank you.

grandjs on Github: Grandjs
grandjs on NPM: Grandjs

grand-validator on Github: grand-validator
grand-validator on NPM grand-validator

Discussion

pic
Editor guide