Programming Articles

Page 2352 of 2547

Understanding the Event driven code execution approach in Node

Shyam Hande
Shyam Hande
Updated on 13-May-2020 212 Views

In earlier example in App.js , we saw how to parse data from request using data and end event.Code snippet below shows the if block for that −if(url === '/username' && req.method === 'POST'){    const requestBody = [];    req.on('data', (chunks)=>{       requestBody.push(chunks);    });    req.on('end', ()=>{       const parsedData = Buffer.concat(requestBody).toString();       const username = parsedData.split('=')[1];       fs.writeFileSync('username.txt', username);    });    //redirect    res.statusCode=302;    res.setHeader('Location', '/');    return res.end(); }In above code block, we have two events (data and end) registered if path matches to ‘/username’ and ...

Read More

Parsing request Body in Node

Shyam Hande
Shyam Hande
Updated on 13-May-2020 3K+ Views

Earlier in simple code example, we saw how to route a request and creating a file to input the test data.Now, we want to save the user entered input data into text file.How Node.js handles incoming request dataNode.js reads data in chunks means it uses streams to read data . Once node completes reading request data, we can proceed to use it for our purpose.First read data in chunks const requestBody = []; req.on(‘data’, (chunks)=>{    requestBody.push(chunks); });We have registred an event called ‘data’ on incoming http request. This event will keep on streaming data and pushes to requestBody const ...

Read More

Redirecting requests in Node.js

Shyam Hande
Shyam Hande
Updated on 13-May-2020 2K+ Views

Now we have an App.js file shown below, we want to redirect the user back to ‘/’ once user name is received by node server. We will store user name in a file named username.txtInitial App.js file −const http = require('http'); const server = http.createServer((req, res)=>{    const url = req.url;    if(url === '/'){       res.write('');       res.write(' Hello TutorialsPoint ');       res.write('       Submit ');       res.write('');       return res.end();    }    res.write('');    res.write(' Hello TutorialsPoint '); ...

Read More

Routing requests in Node.js

Shyam Hande
Shyam Hande
Updated on 13-May-2020 1K+ Views

Routing http requests is important because we want to execute different business rules based on request url and the responses will be different for each routes.Earlier we saw, we can get the url by request.url in node. Simple example of user name input with routes is shown below −const http = require('http'); const server = http.createServer((req, res)=>{    const url = req.url;    if(url === '/'){       res.write('');       res.write(' Hello TutorialsPoint ');       res.write('             Submit ');       res.write('');     ...

Read More

Sending response back from Node.js server to browser

Shyam Hande
Shyam Hande
Updated on 13-May-2020 1K+ Views

App.js −const http = require('http'); const server = http.createServer((req, res)=>{    console.log(req.url, req.method, req. headers); }); server.listen(3000);As shown in above example code, we have request and response parameter object as an argument in createServer method.Response (res) object will be used to send back data to client. It has many properties, some are explained below −res.setHeader(‘Content-Type’, ‘text/html’); this line will set the format of response content o text/html.How to send html content from node.jswrite() function method on response object can be used to send multiple lines of html code like below.res.write(‘’); res.write(‘ Hello TutorialsPoint ’); res.write(‘ Hello Tutorials ...

Read More

Understanding the http requests in Node

Shyam Hande
Shyam Hande
Updated on 13-May-2020 411 Views

App.jsconst http = require('http'); const server = http.createServer((req, res)=>{    console.log(req); }); server.listen(3000);Run it with command: node App.jsWe can see what is inside a request object by opening a browser and navigating to localhost:3000There is lots of information which will get printed on console window. We will see some important properties.Identifying the url from where request came, request method type and headers in request is important.Headers will give us information on host and browser type , accepted response by host etc. Request method can be of any http method type like GET, POST, PUT, DELETE etc.const http = require('http'); const ...

Read More

Understanding the Node lifecycle and event loop in node.js

Shyam Hande
Shyam Hande
Updated on 13-May-2020 2K+ Views

Simple http server in node.js will register an event loop which will keep on listening for http requests.Execution of file containing code for creation of server will work as shown below −node App.js => Start executing script => Code parsing, Register events and functions => event loop=> keeps on running as long as event are registered.This is the single threaded event driven approach of the node.js. For accessing and updating of values in database also works using the event driven approach. Even though it’s a single threaded, it can handle multiple requests at a time due to its speed of ...

Read More

Creating a Node.js server

Shyam Hande
Shyam Hande
Updated on 13-May-2020 659 Views

The mostly used core modules of Node.js are −http − used to launch a simple server, send requestshttps − used to launch a ssl secured http serverpath − used to handle path based on operating systemfs − It’s a file system handling moduleos − its used for os related operationsLets build a simple http server using Node.js −Create a javascript file App.js (name it as you like) in an editor like visual studio code .App.jsconst http = require(‘http’); function reqListener(req, res){    console.log(‘Hello’); } const server = http.createServer(reqListener); server.listen(3000);ExplainationWe used const keyword instead of var or let to import a ...

Read More

How to print pid, info, children, and destroy processes in JShell in Java 9?

raja
raja
Updated on 03-May-2020 198 Views

JShell is a Java Shell tool used to execute simple java statements like classes, methods, interfaces, enums,  and etc.. evaluates it, and prints the result in a command-line prompt.Java has improved Process API to manage and control operating system processes. ProcessHandle interface identifies and provides control of native processes, methods to check processes liveness, and destroy the process. ProcessHandle.Info interface gives an Information snapshot of a process.In the below code snippet, we can print pid, info, children,  and destroy processes of Process API.in JShell tool.Snippetjshell> ProcessHandle currentProcess = ProcessHandle.current(); currentProcess ==> 3960 jshell> System.out.println("Current Process Id: = " + currentProcess.pid()); Current Process Id: = 3960 jshell> ...

Read More

How to get system properties in JShell in Java 9?

raja
raja
Updated on 02-May-2020 553 Views

JShell is a REPL (Read-Evaluate-Print-Loop) tool used to execute simple statements, evaluates it, and displays the result without a main() method. We can start it by simply type "jshell" in command-line prompt.We need to get the system properties by using System.getProperty() and System.getProperties() methods.In the below code snippet, we can able to display the system properties in the JShell tool by using static method property() of System class.Snippet-1jshell> System.getProperty("java.class.path") $1 ==> "C:\Program Files\Java\jdk-9.0.4\lib;C:\json-jars\json.jar;.;C:\json-jars\json-simple.jar;.;C:\json-jars\gson.jar;.;C:\json-jars\commons-io.jar;.;C:\json-jars\jackson-core.jar;.;C:\json-jars\jackson-databind.jar;.;C:\json-jars\jackson-annotations.jar;.;C:\json jars\flexjson.jar;.;C:\json-jars\jackson-dataformat-xml.jar;.;C:\json-jars\stax2-api.jar;.;C:\json-jars\jackson-dataformat-csv.jar;.;C:\json-jars\javax.json.jar;.;C:\json jars\javax.json-api.jar;.;C:\json-jars\jackson-module-jsonSchema.jar;.;C:\json-jars\json-lib.jar;.;C:\json-jars\commons-lang.jar;.;C:\json-jars\commons-logging.jar;.;"In the below code snippet, we have to use the “properties” object that extends Hashtable. So all properties can be listed as key/value pairs in the JShell tool by using ...

Read More
Showing 23511–23520 of 25,466 articles
Advertisements