Skip to main content

node js basic shorts (syntax and pattern)

Updated 2 min readMarkdown
node js basic shorts (syntax and pattern)

এই প্রবন্ধে আমরা জানার চেষ্টা করব কে node JS এঁর কিছু কমন সিনট্যাক্স এন্ড প্যাটার্ন , যা আমরা প্রতিনিয়ত ভুলে যাই ।

কিভাবে নোড জেএস এ সার্ভার তৈরি করতে হয় ?

const http = require("http");
const server = http.createServer((req, res) => {
 res.statusCode = 200;
 res.writable("<h1>hello there!</h1>");
});
server.listen(8080, () => {
 console.log(`server listening on 8080`);
});

কিভাবে আমরা নোড জেএস সার্ভারের পাবলিক ডিরেক্টরিকে পাবলিক করতে পারি ?

const express = require("express");
const app = express();
app.use(express.static(__dirname + "/public"));  
                                     👆👆👆👆 এটা পাবলিক ফোল্ডারে path

নোড জেএস সার্ভারের গ্লোবালি error হ্যান্ডলিং

const express = require("express");
const app = express();
app.use((_req, _res, next) => {
         👆 // কোণ variable ব্যাবহার না হলে এইভাবে underscore দিতে পারি । এতে করে এরর আশার সম্ভবনা থাকে না । কারণে অনেক সার্ভার unused variable না রাখার জন্য বলে । 
 const error = new Error("something went wrong");
 error.status = 404;
 next(error);
});

app.use((error, req, res, next) => {
 if (error.status) {
  return res.status(error.status).json({
   message: error.message,
  });
 }
 res.status(500).json({ message: "something went wrong" });
});

একটা প্রোপার নোড জেএস অ্যাপ্লিকেশানে কি কি বিষয় অবশ্যয় থাকতে হয় ।

  1. /health route [get route]

mongoose বাসিক এন্ড আডভান্স

  1. type define

    সঠিক আপডেট অ তথ্য পেতে এই লিঙ্কে ভিসিট করুন

  2. validator

     const user = Schema({
      email: {
       type: String,                  
       required: true,
       validate: {
        validator: (v) => {
         return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(v);
                   // 👆 অবশ্যয় কল করতে হবে যেখানে আর্গুমেন্ট ভেলু 
        },
        message: `invalid email address ${this.email}`,
        // অথবা
        message: (props) => `invalid email address ${props.value}`,
       },
      },
    
      password: {
       type: String,
       minLength: 2,
       // অথবা
       minLength: [2, "password to short"],
       // ----------------------------------------------
       maxLength: 8,
       maxLength: [8, "password to logn"],
      },
     });
    

About the author

No comments yet. Be the first to comment.

More from this blog