{"componentChunkName":"component---src-components-blogpost-blogpost-jsx","path":"/blog/NodeJS1/","result":{"data":{"site":{"siteMetadata":{"title":"K011"}},"mdx":{"id":"64aa87df-ec4e-541c-80e9-0339234ec54b","body":"var _excluded = [\"components\"];\n\nfunction _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsxRuntime classic */\n\n/* @jsx mdx */\nvar _frontmatter = {\n  \"title\": \"What is node js? and why use it\",\n  \"date\": \"2021-05-26\",\n  \"description\": \"Nodejs\"\n};\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n      props = _objectWithoutProperties(_ref, _excluded);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"h1\", null, \"What is NODE.JS\"), mdx(\"p\", null, \"Node.js is a cross-platform open source environment that runs JavaScript code outside of a browser.\"), mdx(\"p\", null, \"The potential of Node.js is that it allows developers to use JavaScript for server-side development. This provides dynamic web page content before the page is sent to the user's web browser.\"), mdx(\"p\", null, \"Consequently, Node.js unifies web application development around a single programming language, rather than different languages for server-side and client-side scripting.\"), mdx(\"h3\", null, \"Nodejs pros\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Single threaded, based on event drivent\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"perfect for building fast and scalable data-intensive apps\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Companies like netflix uber paypal ebay have started using node in production\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"NPM: huge library of open-source packages available for everyone for free\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Very active developer community\")), mdx(\"h3\", null, \"use nodejs\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"API with database behind it\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Data streaming\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Real-time chat application\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"server-side web application\")), mdx(\"h1\", null, \"Node console and node scripts\"), mdx(\"h3\", null, \"Console\"), mdx(\"p\", null, \"in the node console you can run js expressions with some extras and limitations. example of an extra:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \">1+1\\n2\\n\")), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \">_+3\\n5\\n\")), mdx(\"p\", null, \"And some commands that cannot be executed in js\"), mdx(\"h3\", null, \"script\"), mdx(\"p\", null, \"At first glance nodejs is the same as js. But it is not, for example, we can call modules like \\\"fs\\\" File system that allows us to modify and create and read system files. And a lot of other modules that we can see in the documentation\\nAn example of code is \"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \"const fs = require('fs');\\nconst input= fs.readFileSync('input.txt','utf-8');\\nconsole.log(input);\\n\")), mdx(\"p\", null, \"This code is made in the synchronous way.\\nThe synchronous way has a flaw, which is code locking.\\nAnd one of the reasons why they created nodejs is to fix this problem.\"), mdx(\"p\", null, \"nodejs code:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \"const fs = require('fs');\\nfs.readFile('input.txt','utf-8', (err,data) =>{\\n    console.log(data);\\n });\\nconsole.log('Reading file...);\\n\")), mdx(\"p\", null, \"basically the function runs in the background\"), mdx(\"p\", null, \"Why does it crash, what's wrong with the first code?\\nThis is known as synchronous execution, because the lines are executed one after the other, in the order in which they were written. However, not all instructions provided to the computer must be applied immediately. For example, if you send a network request, the process of executing your code must wait for the data to be displayed before it can be used. In this case, time would be wasted if no other code was executed while waiting for the network request to complete. To solve this problem, developers use asynchronous programming, whereby lines of code are executed in a different order than the one in which they were written. With asynchronous programming, we can execute other code while waiting for lengthy activities such as network requests to finish.\"), mdx(\"h2\", null, \"Asynchronous way:\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Non-blocking I/O model\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"This is why we use so many callback functions in Node.js\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Callnacks != Asynchronous\")), mdx(\"center\", null, mdx(\"img\", {\n    src: \"https://i.imgur.com/vU9fpSd.jpg\",\n    className: \"w-70-l w-100 \"\n  }), \" \"), mdx(\"h2\", null, \"Callbackhell\"), mdx(\"h1\", null, \"Nodejs Modules\"), mdx(\"h2\", null, \"What is a module?\"), mdx(\"p\", null, \"A module is a unit of code organized in files or directories, which can be easily exported for reuse in other parts of the application.\\nThere are 3 types of modules of which the only difference is in the origin\"), mdx(\"h3\", null, \"Built-in modules or Core-modules\"), mdx(\"p\", null, \"These are the native modules of the Node.js API. These are included with node so it is not necessary to install them.\\nThere are many of these, for example \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"fs (filesystem)\"), \" which allows interacting with the system files or console (which is not necessary to import) which provides a console similar to the one in js\\nIn nodejs there are too many modules and all these can be seen from the documentation \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://nodejs.org/en/docs/\"\n  }, \"https://nodejs.org/en/docs/\")), mdx(\"h3\", null, \"Local modules :\"), mdx(\"p\", null, \"These are the modules written by the developers and form a large part of the application as a whole, which are generally structured in this way in order to be a reusable code.\"), mdx(\"h3\", null, \"External modules:\"), mdx(\"p\", null, \" They are, in essence, third party packages distributed through npm (although they can come from other repositories). These packages are installed as dependencies and, although they provide functionality to the application, they should not be included in the repository since they are not part of it.\"), mdx(\"h2\", null, \"Import modules\"), mdx(\"p\", null, \"For this importing modules one can rely on the require method.\\nThere are two cases to use require:\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Built-in or external modules: You only have to pass as argument a string with the name of the module. In the case of built-in they must be installed in the node_modules folder. For node API packages it is not necessary to define it additionally.\")), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \"const fs = require('fs'); // Built-in const lodash = require('lodash');\\n\")), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Local modules: A string with the relative path to the module file is passed as argument. If the argument is a directory, by default it will look for a file named index.js in order to import the module. If there is no such file, the module will be imported as undefined.\")), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-js\"\n  }, \"const adso = require('./modules/adso.js');\\n\")), mdx(\"h1\", null, \"NPM\"), mdx(\"h2\", null, \"What is NPM?\"), mdx(\"p\", null, \"NPM is a package manager for Javascript, that is, it is used to install and manage versions of packages and js libraries.\"), mdx(\"p\", null, \"NPM has long been the benchmark in terms of javascript package managers, but for some time now it has had a competitor: Yarn. Yarn claims that their js library manager is much faster and more powerful, but since most people use NPM I decided to talk about it.\\n##What is NPM for?\\nAs we have seen NPM is a package manager, that means that you can:\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Download js libraries\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Update in case of new version the installed libraries\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Download a specific version of the library\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Manage dependencies between packages\")), mdx(\"p\", null, \"One of the advantages of npm is that all packages are downloaded from a package repository called npmjs.\"), mdx(\"p\", null, \"NPM uses a special file called package.json in which the libraries and their versions are declared. This is very useful because you can have this file with all the libraries you need so that with a simple command they are all downloaded and you don't have to be looking for them in their respective repositories.\"), mdx(\"p\", null, \"The good thing is that you can upload your own libraries to the repository so that if you update the library and upload it to the repository, with a command inside the project where it is used, it will be updated to the new version.\"), mdx(\"h2\", null, \"How to install and use NPM\"), mdx(\"p\", null, \"NPM is built into NodejS, that is, when you download and install nodejs, npm will be installed automatically as npm was made with the intention of being the package and library manager for Nodejs.\"), mdx(\"h3\", null, \"How to download packages\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-bash\"\n  }, \"$npm install\\n\")), mdx(\"p\", null, \"This is the command you will use most often. As the name suggests, it is used to install packages. The packages will be downloaded and put automatically in a folder called node_modules and most often you add this folder to the .gitignore so that it is not uploaded to the git repository.\"), mdx(\"p\", null, \"npm installs locally in the projects. That is to say, when a module is downloaded, it is added to a local project, which is the one that will have it available to include and will have to reinstall it to other projects if it is used again. Although it must be said that there is also the possibility of installing the packages globally in our system.\"), mdx(\"h2\", null, \"Running npm tasks and commands\"), mdx(\"p\", null, \"Another typical use is to execute small npm commands in a more comfortable way. If you have used any framework for the frontend, or you have worked with nodejs, you will have already used these commands.\"), mdx(\"p\", null, \"The commands can be defined in the package.json, in the scripts section:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-json\"\n  }, \"{\\n    \\\"scripts\\\": {\\n        \\\"dev\\\": \\\"node lib/server-development\\\",\\n        \\\"prod\\\": \\\"node lib/server-production\\\"\\n    },\\n}\\n\")), mdx(\"p\", null, \"Just like that, in this case I have created a couple of commands to run in development or in production. To run the commands I simply place myself with the console in the project and run the command I want using:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-bash\"\n  }, \"$npm run <command_name>.\\n\")), mdx(\"p\", null, \"For example:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-bash\"\n  }, \"$npm run dev\\n\")), mdx(\"p\", null, \"The good thing about this is that you can put inside the commands you define any bash code so it can be useful to automate certain tasks in the frontend without having to resort to gulp or webpack.\"), mdx(\"h3\", null, \"Adding dependencies\"), mdx(\"p\", null, \"There are two types of dependencies that you can add to package.json:\\ndependencies: packages that are required during program execution.\\ndevDependencies: packages that are used for development and testing.\\nOne way to add dependencies to your project is to add them to the package.json file directly. For example, if we want to add an execution dependency called slugify and a development dependency called nodemon we can do it as follows:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-json\"\n  }, \"{\\n  \\\"name\\\": \\\"my-super-package\\\",\\n  \\\"version\\\": \\\"1.0.0\\\",\\n  \\\"dependencies\\\": {\\n    \\\"slugify\\\": \\\"1.3.4\\\"\\n  },\\n  \\\"devDependencies\\\". : {\\n    \\\"nodemon\\\" : \\\"1.18.11\\\"\\n  }\\n}\\n\")), mdx(\"p\", null, \"Although also npm install with the --save or --save-dev option. For example:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-bash\"\n  }, \"$npm install slugify --save\\n\")), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-bash\"\n  }, \"$npm install nodemon --save-dev\\n\")));\n}\n;\nMDXContent.isMDXComponent = true;","frontmatter":{"title":"What is node js? and why use it","date":"2021-05-26","description":"Nodejs"}},"allMdx":{"edges":[{"node":{"frontmatter":{"title":"Classification","date":"2021-11-03","description":"Naive Bayes, Discriminant Analysis, Logistic Regression"},"fields":{"slug":"/blog/est4/"}}},{"node":{"frontmatter":{"title":"Regression and Prediction","date":"2021-11-02","description":"REGRESSION"},"fields":{"slug":"/blog/est3/"}}},{"node":{"frontmatter":{"title":"Distribution","date":"2021-11-01","description":"Data distributions"},"fields":{"slug":"/blog/est1/"}}},{"node":{"frontmatter":{"title":"Stats Testing","date":"2021-11-01","description":"Statistical Experiments and significance testing"},"fields":{"slug":"/blog/est2/"}}},{"node":{"frontmatter":{"title":"Recursion","date":"2021-08-26","description":"Algorithm recursion"},"fields":{"slug":"/blog/alg1/"}}}]}},"pageContext":{"slug":"/blog/NodeJS1/","id":"64aa87df-ec4e-541c-80e9-0339234ec54b"}},"staticQueryHashes":["63159454"]}