0% found this document useful (0 votes)
25 views

CS615 Lab5 Ata Gunay

Uploaded by

Ata Günay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

CS615 Lab5 Ata Gunay

Uploaded by

Ata Günay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Laboratory 5 - Ata Gunay

What is the node?


setTimeout
setInterval
clearInterval
_dirname and _filename
Functions
What is a Module in Node.js?
File System
Servers and Streams of Data
NPM
JSON
Postman And Swagger
Express

What is the node?


Resources: https://www.freecodecamp.org/news/what-is-node-js/

Node.js is a JavaScript runtime environment: When you write JavaScript


code in your text editor, that code cannot perform any task unless you execute
(or run) it. And to run your code, you need a runtime environment.

Browsers like Chrome and Firefox have runtime environments. That is why
they can run JavaScript code. Before Node.js was created, JavaScript could
only run in a browser. And it was used to build only front-end applications.

Node.js provides a runtime environment outside of the browser. It's also built
on the Chrome V8 JavaScript engine. This makes it possible to build back-end
applications using the same JavaScript programming language you may be
familiar with.

setTimeout

Laboratory 5 - Ata Gunay 1


Resources: https://nodejs.org/api/timers.html#settimeoutcallback-delay-args

Schedules execution of a one-time callback after delay milliseconds.

The callback will likely not be invoked in precisely delay milliseconds. Node.js
makes no guarantees about the exact timing of when callbacks will fire, nor of
their ordering. The callback will be called as close as possible to the time
specified.

setInterval
Resources: https://nodejs.org/api/timers.html#setintervalcallback-delay-args

Schedules repeated execution of callback every delay milliseconds.

clearInterval
Resources: https://nodejs.org/api/timers.html#clearintervaltimeout

Cancels a Timeout object created by setInterval() .

_dirname and _filename


Resources: https://nodejs.org/api/modules.html#__dirname,
https://nodejs.org/api/modules.html#__filename

The directory name of the current module. This is the same as


the path.dirname() of the __filename .

The file name of the current module. This is the current module file's absolute
path with symlinks resolved.

console.log(__filename);
// Prints: /Users/mjr/example.js

Laboratory 5 - Ata Gunay 2


console.log(__dirname);
// Prints: /Users/mjr

Functions
var sayBye = function(){
console.log('Bye');
};

function callFunction(fun){
fun();
}

callFunction(sayBye); // Bye

// when callFunction executes fun(), it essentially calls sayBye


// resulting in 'Bye' being logged to the console.

What is a Module in Node.js?


Resources: https://www.w3schools.com/nodejs/nodejs_modules.asp

Consider modules to be the same as JavaScript libraries.

A set of functions you want to include in your application.

To include a module, use the require() function with the name of the module:

Use the exports keyword to make properties and methods available outside
the module file.

var counter = function(arr){


return 'There are ' + arr.length + ' elements in this array.';

Laboratory 5 - Ata Gunay 3


}
var adder = function(a,b){
return `The sum of the 2 numbers is ${a+b}`;
}
var pi = 3.1415;
module.exports.counter = counter;
module.exports.adder = adder;
module.exports.pi = pi;

var math = require('./3_math');


console.log(math.counter(['Shaun','Crystal', 'Ryu']));
console.log(math.adder(math.pi, 10));

File System
Resources: https://chat.openai.com/share/75652d74-4980-47b1-9047-
1dc4a04ae043

You can use the fs.readFile() function to read the contents of a file
asynchronously.

You can use the fs.writeFile() function to write data to a file asynchronously.

You can use the fs.appendFile() function to append data to a file


asynchronously.

You can use the fs.rename() function to rename a file asynchronously.

You can use the fs.unlink() function to delete a file asynchronously.

Servers and Streams of Data


Resources: https://www.w3schools.com/nodejs/nodejs_http.asp

To include the HTTP module, use the require() method:

Laboratory 5 - Ata Gunay 4


var http = require('http');

Use the createServer() method to create an HTTP server

If the response from the HTTP server is supposed to be displayed as HTML,


you should include an HTTP header with the correct content type

The function passed into the http.createServer() has a req argument that
represents the request from the client

var http = require('http');


http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(req.url);
res.end();
}).listen(8080);

NPM
Resources: https://www.w3schools.com/nodejs/nodejs_npm.asp

NPM is a package manager for Node.js packages, or modules if you like.

A package in Node.js contains all the files you need for a module.

Modules are JavaScript libraries you can include in your project.

// download
npm install upper-case

// usage
var uc = require('upper-case');

Laboratory 5 - Ata Gunay 5


JSON
JSON is a lightweight data interchange format that is easy for humans to read
and write and easy for machines to parse and generate.

It is based on key-value pairs and is often used to transmit data between a


server and a web application.

JSON data structures resemble JavaScript object literals, making it familiar


and easy to work with in JavaScript environments.

{
"name": "John Doe",
"age": 30,
"email": "[email protected]"
}

Postman And Swagger


Resources: https://testsigma.com/blog/swagger-vs-postman/

Postman and Swagger serve different but complementary purposes in the


realm of API development and testing.

Postman is primarily used for API testing and automation, allowing developers
to create, organize, and execute API requests, monitor responses, and
collaborate with team members.

On the other hand, Swagger, also known as the OpenAPI Specification, is


primarily employed for API design, documentation, and testing.

It provides a structured way to define API endpoints, parameters, and


responses, fostering consistency in API development and clear documentation
for both developers and consumers.

Laboratory 5 - Ata Gunay 6


Express
Express is a minimalist web application framework for Node.js, designed for
building web applications and APIs.

It provides a robust set of features for handling HTTP requests and responses,
routing, middleware, and more.

Express simplifies the process of building web servers and APIs by providing a
clean and intuitive API for handling common tasks.

It is highly extensible, allowing developers to easily add additional functionality


through middleware and third-party packages.

// Express with routing

const express = require('express');


const app = express();

// Define routes
app.get('/', (req, res) => {
res.send('Home Page');
});

app.get('/about', (req, res) => {


res.send('About Page');
});

// Start server
app.listen(3000, () => {
console.log('Server is listening on port 3000');
});

Laboratory 5 - Ata Gunay 7

You might also like