Wednesday, September 16, 2020

Cập nhật nodejs với nvm

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
nvm ls // local node versions
nvm ls-remote // remote node versions
nvm install 12.8.8 // install node v12.8.8
nvm use 10.6.7 // switch to node v10.6.7

Monday, September 14, 2020

Nodejs Starter - Javascript Boilerplates to start fast

 Hello Coders,

Usually, when we start a new project, the codebase tends to repeat over and over among different projects. Instead of reinventing the wheel, all the time can be a good solution to use a starter kit, already equipped with a few basic tools and features.

This article presents a shortlist with Node.js starters qualified for production use, based on a few necessary characteristics:

  • clean code structure that follows proper coding standards
  • documented
  • actively supported
  • a minimum validation (Github Stars, Number of Downloads)
  • open source
  • permissive license ( MIT, Apache )

In my opinion, this shortlist with requirements qualifies a NodeJS starter to become a real candidate for a production app.

This list includes only a few Nodejs starters, used by me in different projects in the last 2 years. If you know a new killer project, unlisted here, please suggest it in the comments. Thank you!


Nodejs Starter / Vue Argon Design

Nodejs Starter - Vue Argon Design is a Full-Stack Nodejs starter equipped with a fully usable Vue.js UI, coded on top of the Argon Design System design. The app uses a decoupled architecture, where the Vue.js frontend communicates with the Nodejs through secure ajax calls.

Nodejs Starter, React Material Kit - Gif animated intro.


Nodejs Starter / React Material Kit

Nodejs Starter - React Material Kit is a Full-Stack Nodejs starter equipped with a fully usable React UI, coded on top of the Material Kit design. The app uses a decoupled architecture, where the React frontend communicates with the Nodejs through secure ajax calls.

Nodejs Starter, React Material Kit - Gif animated intro.


Nodejs Starter Hackathon

Nodejs Starter Hackathon is a project with 26k+ stars on Github, equipped with many useful features like:

  • MVC structure
  • Authentification: local (user/pass), OAuth (1.0, 2.0)
  • SaaS stylesheets, Bootstrap4 integrated
  • Forms powered by Mailgun and Sendgrid
  • Integrations for Paypal, Twitter, LinkedIn

This starter can be overwhelming for a beginner, but this fact can be easily compensated by reading the docs and interact with the project community.

Nodejs Starter hackathon - Image


Nodejs Starter API Kit

Nodejs Starter API Kit is a Boilerplate for authoring data API backends with Node.js and GraphQL. With more than 2.8k Github starts, this boilerplate is definitively a winner if your future project replaces the old school REST API with GraphQL. Features:

  • Common Node.js stack: Express, Passport, Session, Cors
  • PostgresSQL as DMBS, along with Redis
  • Localization and translations
  • Nodemailer, Handlebars templating
  • Deployment: via Docker

Starter Links


Nodejs Starter Serverless

Nodejs Starter Serverless is a Node.js starter for the Serverless Framework with async/await and unit test support. The project is actively supported by Frank Wang with 450 Github stars. This starter got my attention mainly for the serverless concept, which is a rising concept in web development nowadays. Features:

  • Integrate serverless-webpack plugin
  • ES7 syntax in handler functions
  • Support for unit tests (via Jtest)

Nodejs Starter Serverless Live sample hosted on AWS, and a sample of the code executed when we click on the link:

export const hello = async (event, context) => {
  return {
    statusCode: 200,
    body: JSON.stringify({
      message: `Go Serverless v1.0! ${(await message({ time: 1, copy: 'Your function executed successfully!'}))}`,
      input: event,
    }),
  };
};

const message = ({ time, ...rest }) => new Promise((resolve, reject) =>
  setTimeout(() => {
    resolve(`${rest.copy} (with a delay)`);
  }, time * 1000)
);

To start playing with this Nodejs starter, here is the list with requirements:


Nodejs Starter Express Bootstrap

Nodejs Starter Express Bootstrap is a lightweight Bootstrap NodeJS boilerplate built with ExpressJS 4, MongoDB/Mongoose, Authentication with Passport.js, Jade and GruntJS as Task Automation.

The app is well organized and actively supported by a freelancer. The demo link, hosted on Heroku, seems to be dead. The app can be successfully built on (at least) on Windows10 and Ubuntu 18.04.

Not here production-ready Nodejs starter you know? Please suggest in the comments.


Resources

Thank you!

The Best Node.js Boilerplate to Speed Up Your Project Development

 

What is Boilerplate?

Boilerplate is the term used to describe sections of code that can be included in many places with little or no alteration. It is more often used when referring to languages which are considered verbose, i.e. the programmer must write a lot of code to do minimal jobs. Boilerplate code is a piece of code which can be used over and over again or we can say it, a piece of reusable code.

Listing
Here is a list of boilerplate code you should consider when starting your Node.js project:

  1. Sahat Boilerplate

The first is Sahat boilerplate. Sahat for Node.js web applications is the most complete boilerplate and comes with a complex package. Sahat makes it as generic and reusable as possible to cover most use cases of hackathon web apps, without being too specific. In the worst case, you can use this as a learning guide for your projects. They have over 24k stars on GitHub.

2. Kriasoft Boilerplate

Kriasoft creates a Node.js API starter; this is a boiler and a tool for creating API data backend with Node.js, GraphQL, and bundle with Docker. This boilerplate is the most suitable for developing the GraphQL API endpoint. It can be built as an independent microservice or for creating a web front-end back-up and mobile application. If you are interested in GraphQL, this boilerplate is recommend to start your project.

3. Madhums’ Boilerplate

Madhums’ boilerplate is a boilerplate application for building web apps using Express, Mongoose, and Passport. Madhums’ boilerplate comes with MVC base pattern to implement the project. If you are familiar with MVC pattern it can be easier to switch from another language to Node.js

4. Icebob Boilerplate

Icebob boilerplate is a full-stack web app boilerplate project with VueJS, ExpressJS, and MongoDB. Inspired by Sahat boilerplate, it focused on a starter project which contains common functions like user signup, login, OAuth, and user profile. So, we can create a new web app and only need to develop the business logic.

5. Talyssonoc Node API boilerplate

The last one is Talyssonoc Node API boilerplate. This boilerplate was built for creating backend API. The folder structure and logical architecture of this boilerplate focused on the separation of concerns based on domain-driven design and clean architecture. Instead of using the MVC pattern, this boilerplate has layers inside the src folder. Each of the folder layers is scoping by a namespace on the concern it is about (like the user, errors, logging and so on). For detailed information about the structure behind the boilerplate, read this. Unlike the others, this API base boilerplate by Talyssonoc is the simplest way to install the API project.


Recap

All boilerplates have advantages and disadvantages, each depending on the type of project. You can choose Sahat or Icebob to create an application with back end and front end support. Talyssonoc, Kriasoft, or Madhums boilerplate is suitable to create an API back end. You can choose Madhums boilerplate if you’re familiar with the MVC pattern, but if you’re looking for clean architecture and scalability, go for Talyssonoc.

Saturday, September 12, 2020

CommonJS …what, why and how

 What is CommonJS? CommonJS is a module formatting system. It is a standard for structuring and organizing JavaScript code. CJS assists in the server-side development of apps and it’s format has heavily influenced NodeJS’s module management.

Image for post
“boy wearing gray vest and pink dress shirt holding book” by Ben White on Unsplash
Image for post
Image for post

Wednesday, January 15, 2020

Get HTTP request body data using Node.js

For example, to get the body of this request:
const axios = require('axios')

axios.post('https://whatever.com/todos', {
  todo: 'Buy the milk'
})
Using body-parser on Express
const express = require('express')
const app = express()
const port = 3000

const bodyParser = require('body-parser')

app.use(
  bodyParser.urlencoded({
    extended: true
  })
)

app.use(bodyParser.json())
app.post('/', (req, res) => {
console.log(req.body)
res.send('Hello World!')
})

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

Using  express.json()
const express = require('express')
const app = express()
const port = 3000
app.use(express.json());

app.post('/', (req, res) => {
console.log(req.body)
res.send('Hello World!')
})

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

Sunday, January 12, 2020

Debug with node

Cách 1: console.log

Cách 2:

  1. chạy node inspect
  2. vào chrome://inspect

muốn debug chỗ nào thì viết chữ "debugger" chỗ đó
có thể sử dụng được cả console cua chrome