How to install Deno on Ubuntu Linux

Deno tutorial for beginners: how to install Deno on Windows


Deno is a Type Script and JavaScript run time which offer more security and flexibility than Nodejs. It is created in Rust language, by the same Nodejs developer . The frame work has the following features

  • Secure by default. No file, network, or environment access, unless explicitly enabled.
  • Supports TypeScript out of the box.
  • Ships only a single executable file.
  • Has built-in utilities like a dependency inspector (deno info) and a code formatter (deno fmt).
  • Has a set of reviewed (audited) standard modules that are guaranteed to work with Deno:

Install Deno

You can install Deno on Linux using curl . In Ubuntu the curl may not available, so you have to install it first.

sudo apt install curl

After that we can complete Deno installation

curl -fsSL https://deno.land/x/install/install.sh | sh

The demo path will be set as home/username/.deno/bin on Ubuntu

It will install and setup and the path for Deno, Now try to check version of Deno

deno -v

How to resolve sequelize-JSON.parse error in Node

How to resolve JSON.Parse error in sequelize


When I start with Sequelize ORM end up with the body-parser error. Usually we define body parser using app.use(bodyparser.json()), which globally declare parser functionality for all routes.

SyntaxError: Unexpected token in JSON at position 0
at JSON.parse ()
at createStrictSyntaxError (E:\NodeProjects\sequelize-api -mysql\node_modules\body-parser\lib\types\json.js:158:10)
at parse (E:\NodeProjects\sequelize-api -mysql\node_modules\body-parser\lib\types\json.js:83:15)
at E:\NodeProjects\sequelize-api -mysql\node_modules\body-parser\lib\read.js:121:18
at invokeCallback (E:\NodeProjects\sequelize-api -mysql\node_modules\raw-body\index.js:224:16)
at done (E:\NodeProjects\sequelize-api -mysql\node_modules\raw-body\index.js:213:7)
at IncomingMessage.onEnd (E:\NodeProjects\sequelize-api -mysql\node_modules\raw-body\index.js:273:7)
at IncomingMessage.emit (node:events:388:22)
at endReadableNT (node:internal/streams/readable:1305:12)
at processTicksAndRejections (node:internal/process/task_queues:80:21)

Here is where sequelize findAll() like functionality collides and the above error log will be populated

Solution

We can apply route specific parser to fix this mess. Let’s see how

Before the fix

For simplicity I have omitted some of the lines in index.js (Node project file). Before the fix our routes look like this

...
app.use(bodyParser.urlencoded({extended:false})) 
app.use(bodyParser.json())

app.post("/todo",  (req, res, next) => {
    const {item,description}=req.body
    Todo.create({item:item,description:description})
    .then((model) => {
      res.status(200).send(model);
      })
      .catch((e) => {
        res.status(400).send("Error:" + e);
      });
  });

app.get("/todo", (req, res, next) => {
  Todo.findAll()
    .then((model) => {
        res.json({
            error: false,
            data: model
        })
    })
    .catch(error => res.json({
        error: true,
        data: [],
        error: error
    }))
});


After the fix

After the removal of the global json parser it will look like this

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

app.post("/todo", bodyParser.json(), (req, res, next) => {
    const {item,description}=req.body
    Todo.create({item:item,description:description})
    .then((model) => {
      res.status(200).send(model);
      })
      .catch((e) => {
        res.status(400).send("Error:" + e);
      });
  });

app.get("/todo", (req, res, next) => {
  Todo.findAll()
    .then((model) => {
        res.json({
            error: false,
            data: model
        })
    })
    .catch(error => res.json({
        error: true,
        data: [],
        error: error
    }))
});


we have added the parser function of the bodyParser to Post routes only and the problem solved

Deno : next generation Node

About Deno TypeScript Runtime


Deno is a Type Script and JavaScript run time which offer more security and flexibility than Nodejs. It is created in Rust language, by the same Nodejs developer . The frame work has the following features

  • Secure by default. No file, network, or environment access, unless explicitly enabled.
  • Supports TypeScript out of the box.
  • Ships only a single executable file.
  • Has built-in utilities like a dependency inspector (deno info) and a code formatter (deno fmt).
  • Has a set of reviewed (audited) standard modules that are guaranteed to work with Deno:

Deno didn’t download all the dependencies and dependencies of decencies as Node does. It uses URL based dependency management.

Wanna learn more about Deno, jump to Deno land

In the upcoming tutorials, I will show you how to’s of Deno.

How to extract query string from url parameter using JavaScript

How to access the query string/url parameter of a url in JavaScript


Query string are helpful when need to pass values to a page. It start with ? and multiple statement can be added using &

We can also use window.location.href to grab the current page URL in JavaScript

Here is an example query string

example.com/?pid=1001&type=electronics

Using JavaScript we can access the query string easily with window.location.search. Let’s have have query search example

const queryString = window.location.search;
 console.log(queryString);
 // ?pid=1001&type=electroni        

Find a Parameter

To find/sear a URL Parameter say PID , we need to use URLSearchParams and using the output we can access the param value too as follows

const urlParams = new URLSearchParams(queryString);
 const pid = urlParams.get('pid')
 console.log(pid); 

Following JavaScript post may help you

How to access URL of current page in JS

How to access current page url in JavaScript


In JavaScript we can access window/browser properties using window object. This can be helpful when dealing with query string.

Here is a quick example how you can access the URL of the current page in JavaScript

cost url =widnow.location.href
console.log(url)
Out Put
http://wordpress.com

How to pass python list to JavaScript in Flask app

How to pass a python list to JavaScript with ninja template in Flask web app


You can’t use Python list directly into JavaScript, JavaScript lislt/array is not compatible with Python or wise versa. So what to do.

Solution

I have a list of names, which I want pass to a JavaScript in a Flask ninja template. Ninja template allow use to safely convert the python list to Json string then we can pass it to the JavaScript function / just JavaScript

<script type="text/javascript">

        dn = {{ distabr|tojson }}
</script>

You can’t use same route variable in multiple places in same template, it may not serve the purpose

In JavaScript Functions

Suppose you have separate JavaScript function files for some functionality like drawing analytic plots and pie diagram, or some donut chart. So how do we pass value to those functions ?

You can create a JavaScript as above, in your current template, then in function create a script variable to use it as follows.

function init_echarts() {
    var distNames=dn;
}

That’s all you need to know to embed some morris diagram to your flask app.

You should look at Morris JS , it is free and easy to use JavaScript charts library.