Found 285 Articles for Node.js

Deleting records in MySQL using Nodejs

Mayank Agarwal
Updated on 27-Apr-2021 09:21:34

563 Views

After insertion, we need to delete records as well. The records should can be deleted based upon an identifier from the database table. You can delete records from table using "DELETE FROM" statement.We can delete the records from MySql DB in two ways −Static Deletion - In this type of deletion, we give a prefixed filter value to deleteDynamic Deletion – In this type of deletion, we ask for an input before deletion and then delete upon its basis.Before proceeding, please check the following steps are already executed −mkdir mysql-testcd mysql-testnpm init -ynpm install mysqlThe above steps are for installing ... Read More

Creating a MySQL table using Node.js

Mayank Agarwal
Updated on 27-Apr-2021 09:17:53

647 Views

Generally, NoSQL databases (like MongoDB) are more popular among the Node developers. However, it totally depends upon your usecase and choice to choose any DBMS from different database options present. The type of databse you choose mainly depends upon one's project's requirements.For example, if you need table creation or real-time inserts and want to deal with loads of data, then a NoSQL database is the way to go, whereas if your project deals with more complex queries and transactions, an SQL database will make much more sense.In this article, we will explain how to connect to a MySQL and then ... Read More

Creating a MySQL Table in NodeJS using Sequelize

Mayank Agarwal
Updated on 27-Apr-2021 09:15:06

2K+ Views

Introduction to SequelizeSequealize follows the promise-based Node.js ORM for different servers like – Postgres, MySQL, MariaDB, SQLite, and Microsoft SQL Server.Following are some of the main features of NodeJS sequelize −Transaction SupportRelationsEager and Lazy LoadingRead Replication and more...Connecting to MySQL using SequelizeWe need to establish a connection between MySQL and Node.js using Sequelize.After creating a successful connection with sequelize, we would require the following three files for configuration. Please carefully create the following files in their respective folders only.SequelizeDemo > application.jsThis will be our root file which will hold the actual logic.SequelizeDemo>utils>database.jsThis will hold all the connection details to MySQL.SequelizeDemo>models>user.jsThis ... Read More

Connecting MongoDB with NodeJS

Mayank Agarwal
Updated on 27-Apr-2021 09:11:43

1K+ Views

Introduction to mongodb.connectThis method is used to connect the Mongo DB server with our Node application. This is an asynchronous method from MongoDB module.Syntaxmongodb.connect(path[, callback])Parameters•path – The server path where the MongoDB server is actually running along with its port.•callback – This function will give a callback if any error occurs.Installing Mongo-DBBefore proceeding to try connect your application with Nodejs, we need to setup our MongoDB server first.Use the following query to install mongoDB from npm.npm install mongodb –saveRun the following command to set up your mongoDB on the specific localhost server. This will help in creating connection with the ... Read More

Async Copy in fs-extra - NodeJS

Mayank Agarwal
Updated on 27-Apr-2021 09:08:43

630 Views

Introduction to Async copyThis method copies files or directories from one location to another location. The directory can have sub-directories and files.Syntaxcopy(src, dest[, options][, callback])Parameterssrc – This is a string paramter which will hold the source location of the file or directory that needs to be copies. If the location is a directory, it will copy everything inside of the directory instead of whole directory.dest – This will hold the destination location where the files/directories will be copies. If src is a files, dest cannot be a directory.options −overwrite – If set to true, existing files or directories will be ... Read More

Styling html pages in Node.js

Shyam Hande
Updated on 13-May-2020 13:37:19

6K+ Views

In html files we can simply add style in head section −                    //add css code           We can add inline css styles as well in html directly.Generally css is separated from the html code. Third option add css is to include a css file .How to serve static files in Node.js?Generally css files are added with below tag −     Express js provides a middleware to seve static file. This middleware gives read access to given folder.app.use(express.static(path.join(__dirname, ‘public’)));path: its our core module ... Read More

Serving html pages from node.js

Shyam Hande
Updated on 13-May-2020 13:34:35

2K+ Views

So far we sent html code directly from the send(0 function in response object. For sending larger code, we definitely require to have a separate file for html code.sendFile() function−Response object gives a sendFile() function to return a html file to client.How to provide path to html file in sendFile() ?We import the path core module of node.js.const path = require(‘path’);path has a join function . __dirname is a global variable which holds the actual path to project main folder.path.join(__dirname, ‘views’, ‘add-user.html’); This will refer to the actual file location of add-user html code.App.jsconst http = require('http'); const express = ... Read More

Adding 404 page in express

Shyam Hande
Updated on 13-May-2020 13:29:27

819 Views

Now we have a App.js and route.js for handling routes. For any other http requests for which we have not added any request handling will results into an error page. Example for url ‘test’ −App.jsconst http = require('http'); const express = require('express'); const bodyParser = require('body-parser'); const route = require('./routes'); const app = express(); app.use(bodyParser.urlencoded({extended: false})); app.use(route); const server = http.createServer(app); server.listen(3000);Showing meaningful error message on incorrect url’s−We can add a all catch middleware for incorrect url at the end of all middleware’s in App.js −const http = require('http'); const express = require('express'); const ... Read More

How to use express router

Shyam Hande
Updated on 13-May-2020 13:27:17

2K+ Views

In earlier examples, we wrote all routing code in a single file App.js. But in real world scenarios, we have to split the code into multiple files.We can create separate files and import them but express gives a router mechanism which is easy to use.Create a separate file called route.js (name can be anything)Create router using express −const express = require('express'); const router = express.Router();exporting router −module.exports = router;Adding routing functions −router.get('/add-username', (req, res, next)=>{    res.send(' Send '); });  router.post('/post-username', (req, res, next)=>{    console.log('data: ', req.body.username);    res.redirect('/'); });Similar to functions we used in App.js ... Read More

Using post request in middleware in express

Shyam Hande
Updated on 13-May-2020 13:25:04

1K+ Views

We used use() function to execute middleware’s . Example we used below will be executed for both http GET and POST method −const http = require('http'); const express = require('express'); const bodyParser = require('body-parser'); const app = express(); app.use(bodyParser.urlencoded({extended: false})); app.use('/', (req, res, next)=>{    next(); }); app.use('/add-username', (req, res, next)=>{    res.send(' Send '); }); app.use('/post-username', (req, res, next)=>{    console.log('data: ', req.body.username);    res.redirect('/'); }); app.use('/', (req, res, next)=>{    res.send(' first midleware: Hello Tutorials Point '); }); const server = http.createServer(app); server.listen(3000);How to restrict middleware only for GET or only for POST http method ... Read More

Advertisements