Categories
Node.js Tips

Node.js Tips — File Operations, Command Line Arguments, and More

Like any kind of apps, there are difficult issues to solve when we write Node apps.

In this article, we’ll look at some solutions to common problems when writing Node apps.

Pass Command-Line Arguments to a Node.js Program

We get command line arguments by using the process.argv property.

It’s an array with all the arguments including node and the script name.

'node' is always the first entry.

The script name is the 2nd entry.

The rest of the arguments are the remaining entries.

For instance, if we have:

node foo.js one two=three four

Then node is the first entry of process.argv , foo.js is the 2nd, one is the 3rd, and so on.

Writing files in Node.js

We can use the writeFile method to write a file in a Node app.

For instance, we can write:

const fs = require('fs');

fs.writeFile("/foo.txt", "hello world!", (err) => {
  if (err) {
    return console.log(err);
  }
  console.log("file saved");
});

We call writeFile to write the file.

The first argument is the path to the file.

The 2nd is the content of the file.

The callback is called when the file writing process ends.

err is the error object and it’s defined when an error is encountered.

There’s also a synchronous version call writeFileSync .

We can use it by writing:

fs.writeFileSync('/foo.txt', 'hello world');

The first argument is the file path.

The 2nd is the content.

How to Debug Node.js Applications

We can use the node-inspector package to debug Node apps.

We can install it by running:

npm install -g node-inspector

to install it globally.

Then we run node-debug app.js to run it with node-inspector which lets us debug it with breakpoints, profiling, etc.

We can also run node inspect app.js to run app.js with the debugger.

Read Environment Variables in Node.js

We can read environment variables in Node apps by using the process.env object.

For instance, if we have the environment variable FOO , we can write:

process.env.FOO

Remove Empty Elements from an Array in Javascript

To remove empty from a JavaScript array, we can use the filter method.

For example, we can write:

const arr = [0, 1, null, undefined, 2,3,,,,6];
const filtered = arr.filter((el) => (el !== null || typeof el !== 'undefined'));

This will return an array created from arr with all the null and undefined removed.

Using async/await with a forEach loop

We can use async and await with the for-of loop.

For instance, we can write:

const printFiles = async (filePaths) => {
  for (const file of filePaths) {
    const contents = await fs.readFile(file, 'utf8');
    console.log(contents);
  }
}

We use the promisified version of readFile to read the file inside each iteration of the for-of loop.

To run promises in parallel, we can use the Promise.all method.

For instance, we can write:

const printFiles = async (filePaths) => {
  await Promise.all(filePaths.map(async (file) => {
    const contents = await fs.readFile(file, 'utf8')
    console.log(contents)
  }));
}

We call map to filePaths to map the paths to promises.

Then we call Promise.all on the array of promises.

Get a List of the Names of All Files Present in a Directory in a Node App

We can get a list of names of all files in a directory by using the readdir or readdirSync method.

For example, we can write:

const fs = require('fs');

fs.readdir('/tesrDir', (err, files) => {
  files.forEach(file => {
    console.log(file);
  });
});

files have the array of file strings, buffer, or directory entries.

To read a directory synchronously, we can write:

const fs = require('fs');

fs.readdirSync('/testDir').forEach(file => {
  console.log(file);
});

We read a directory synchronously.

Parse JSON with Node.js

We can require JSON files or parse them with JSON.parse .

For example, we can write:

const config = require('./config.json');

or:

const config = require('./config');

The extension is optional.

Also, we can use JSON.parse to parse JSON strings.

Conclusion

We can get command-line arguments with process.argv .

Environment variables are read into the process.env object.

require can be used to import JSON files.

The fs module has methods to read and write files.

The for-of loop can run promises sequentially.

Categories
Koa Nodejs

Validating Request Data with Koa

Koa is a small framework that lets us create backend apps that run on the Node.js platform.

In this article, we’ll look at how to validate request data type with Koa.

Validating Request Data Type Using the request.is Method

We can call the ctx.is method with the string for the data type that we’re checking for to check if the data type from the Content-Type header’s value matches what we have in the argument.

It can take one or more data types. For instance, we can use it as follows:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  if (ctx.is('text/*', 'text/html')){
    ctx.body = 'valid';
  }
  else {
    ctx.body = 'invalid';
  }
});

app.listen(3000);

In the code above, we called the ctx.is method with the strings for 2 data types, which are ‘text/*’ and ‘text/html’ to check if the incoming request have any of these values as the value of its Content-Type header.

If one of these values are includes, then it returns 'valid' as the response body.

Otherwise, it returns 'invalid' .

Content Negotiation

Content Negotiation is a mechanism that’s used to serve different representations of a resource from the same URL. We can use the user agent that can specify which is best suited for the user.

When making requests to the client, we can use the Accept , Accept-Charset , Accept-Encoding , or Accept-Language headers in the request to let the server know what kind of data the client accepts.

With that data, the server can return a response with the most suitable kind of data for the client.

For instance, we can use it as follows:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  if (ctx.accepts('html')){
    ctx.body = 'accepts html';
  }
  else if (ctx.accepts('application/json')){
    ctx.body = 'accepts json';
  }
  else if (ctx.accepts('image/png')){
    ctx.body = 'accepts png';
  }
  else {
    ctx.body = 'accepts everything';
  }
});

app.listen(3000);

In the code above, we called the ctx.accepts method with the content-type string that we want to check for in those headers listed above, so that we can return the appropriate response for it.

Then when we make a request to our app with one of those headers and application/json as its value, then we get the string 'accepts json' returned as the response.

Checking for the Encoding that the Client Accepts

We can use the ctx.acceptsEncodings method to check what kind of encoding the client accepts.

It takes one more encoding type strings as its argument or it can be one array of encoding type strings.

For instance, we can use it as follows:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  if (ctx.acceptsEncodings('gzip') === "gzip"){
    ctx.body = 'accepts gzip';
  }
  else {
    ctx.body = 'does not accept gzip';
  }

});

app.listen(3000);

In the code above, we called the ctx.acceptEncodings method with the argument 'gzip' . The method returns the encoding that’s the best match.

Then if we make a request to our app with the Accept-Encoding header gzip, deflate, br , we’ll get that the response 'accepts gzip' from the app.

If no arguments are given, then all accepted encodings are returned.

Checking the Character Set That’s Accepted By the Client

We can call the acceptsCharsets to check for the character sets that are accepted by the client.

It also takes one or more string for the character set names that are accepted or an array of strings with those character set names.

For instance, we can use it as follows:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {

  if (ctx.acceptsCharsets('utf-8') === 'utf-8'){
    ctx.body = 'accepts utf-8';
  }
  else {
    ctx.body = 'does not accept utf-8';
  }

});

app.listen(3000);

The code above is similar to the acceptEncodings code.

If we make a request that doesn’t include the Accept-Charset header that includes utf-8 , then we’ll get ‘does not accept utf-8’ returned as the response.

Otherwise, we’ll get 'accepts utf-8' returned whether we made the request explicitly with the Accept-Charset header with utf-8 as the value or not.

Checking the Language of the Request

The ctx.acceptsLanguages let us check the language of the request according to the Accept-Language header.

It’s similar to the other 2 methods above with the arguments that it takes and what it returns.

Get Any Header Field

We can get any request header field with the ctx.get method. For instance, we can write the following to do that:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  ctx.body = ctx.get('foo');
});

app.listen(3000);

In the code above, we called the get method with the 'foo' header name. It returns the value of the 'foo' header if it exists.

Then when we make a request with the foo header with value bar , we’ll see 'bar' returned as the response.

Conclusion

We can use the ctx.is method to validate data types that are sent to the server.

To do content negotiation, we can use the accepts method. To check the language, encoding, or character set, there’re methods to do that.

Finally, we can get any request header value with the get method.

Categories
Koa Nodejs

Getting Request Data with Koa

Koa is a small framework that lets us create backend apps that run on the Node.js platform.

In this article, we’ll look at how to receive request data with Koa.

Getting the Content-Type Header of a Request

We can use the ctx.request.type property to get the Content-Type header of the request.

For instance, we can write the following code to display the value of that on the screen:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  ctx.body = ctx.request.type;
});

app.listen(3000);

Then if we send our request with the Content-Type header’s value set to application/json , then we’ll get that displayed on the screen when we get the response.

Get the Character Set of the Request

The ctx.request.charset property has the request character set.

Getting the Query String From a Request URL

The ctx.request.query property has query string parsed into a key-value pair.

Each query parameter of the query string is parsed into a key-value pair of an object.

For instance, we can use it as follows:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  ctx.body = ctx.request.query;
});

app.listen(3000);

In the code above, we just set the query property as the value of the ctx.body .

Then when we make a request to https://CraftyGrouchyCables–five-nine.repl.co?a=1&b=2, we get the following response:

{
    "a": "1",
    "b": "2"
}

As we can see, the key-value pairs from the query string are parsed as the key-value pairs of the object set as the value of ctx.request.query without doing anything ourselves.

The ctx.request.query property is also a setter, so we can set it to something else. However, it doesn’t support nested objects.

Check if the Request Cache is Fresh, or if its Content Has Changed or Not

We can check the request cache’s freshness by using the ctx.fresh property.

For instance, we can use it as follows:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  ctx.status = 200;
  ctx.set('ETag', 'foo');
  if (ctx.fresh) {
    ctx.status = 304;
    console.log('fresh');
    return;
  }
  ctx.body = 'foo';
});

app.listen(3000);

In the code above, we check if the request cache is fresh by using the ctx.fresg property. If it’s fresh, then we’ll see the fresh string logged.

Otherwise, we won’t see it logged. If we make more than one request to our app, then we won’t see it logged since the cache has been populated with data that haven’t change we always return the same response.

There’s also a ctx.stale property to check that the opposite status of ctx.fresh is true .

Get the Protocol of Our Request

We can use the ctx.request.protocol property to get the request protocol. It’s either https or http .

If app.proxy is set to true , it can also check the X-Forwarded-Proto request header for the protocol.

Get the IP Address of the Device Making the Request

The ctx.request.ip and ctx.request.ips can get us the remote address of the device making the request.

The ips property gets us the IP address of all the devices that the request went through to make it to our app.

For instance, we can use the ip property as follows:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  ctx.body = ctx.request.ip;
});

app.listen(3000);

Then when we make a request to our app, we get the IP address of the device that made the request.

We can also use the ips property if app.proxy is set to true . For instance, we can use it as follows:

const Koa = require('koa');
const app = new Koa();

app.proxy = true;

app.use(async (ctx, next) => {
  ctx.body = ctx.request.ips;
});

app.listen(3000);

Then we’ll see an array of IP addresses that the request went through to reach our Koa app.

Photo by Carles Rabada on Unsplash

Get the Request Subdomains

We can get the subdomains of the request URL by using the subdomain property.

For example, we can use that as follows:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  ctx.body = ctx.request.subdomains;
});

app.listen(3000);

Then if our app lived in https://CraftyGrouchyCables–five-nine.repl.co, we get that the subdomains property will return [“craftygrouchycables — five-nine”] so we’ll get that displayed on the screen as the response value.

Conclusion

We can use the ctx.request property to get the IPs and the subdomains of the requested URL.

The protocol and query string are also available. Query strings are automatically parsed into an object.

Categories
Koa Nodejs

Sending Response with Koa

Koa is a small framework that lets us create backend apps that run on the Node.hs platform.

In this article, we’ll look at how to send various kinds of responses with Koa.

Sending the Body

To send the response body, we can set the body attribute of ctx . For instance, we can send a response body as follows:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  ctx.body = 'foo';
});

app.listen(3000);

In the code above, we set the ctx.body property to 'foo' . Therefore, that’s what we’ll get when we go to the / route with our browser or make a request to it with an HTTP client.

Sending Response Header

We can send responses in our Koa code by setting the ctx.response property.

To set the header, we can set the response header with the ctx.set method.

For instance, we can use it as follows:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  ctx.set('Access-Control-Allow-Origin', '*');
  ctx.set('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  ctx.set('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS');
  ctx.body = 'hello';
});

app.listen(3000);

In the code above, we called ctx.set to set various headers, including the Access-Control-Allow-Origin header, Access-Control-Allow-Headers header, and Access-Control-Allow-Methods header.

Once we make a request to / , we’ll see those headers in HTTP clients like Chrome or Postman.

Sending Response Status Code

We can send response status codes by setting the status code value to the responbse.status property.

For instance, we can do that as follows:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  ctx.status = 202;
  ctx.body = 'accepted';
});

app.listen(3000);

In the code above, we set the ctx.status ‘s value to 202. So when we make a request to the / route, we’ll get the 202 status code from the request.

Some common response codes that are send include:

  • 200 — OK
  • 201 — created
  • 202 — accepted
  • 203 — non-authoritative information
  • 204 — no content
  • 301 — move permanently
  • 302 — found
  • 303 — see other
  • 307 — temporary redirect
  • 308 — permanent redirect
  • 400 — bad request
  • 401 — unauthorized
  • 403 — forbidden
  • 404 — not found
  • 405 — method not allowed
  • 406 — not acceptable
  • 422 — unprocessable entity
  • 500 — internal server error
  • 501 — not implemented
  • 502 — bad gateway
  • 504 — gateway timeout

Sending Headers

We can set the ctx.response.lastModified property to the date value that we want.

For instance, we can set that as follows:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  ctx.response.lastModified = new Date(2020, 0, 1);
  ctx.body = 'foo';
});

app.listen(3000);

In the code above, we set the lastModified property to January 1, 2020, so when we make the request to the / route, we’ll get the Last-Modified from the response with the value of Wed, 01 Jan 2020 00:00:00 GMT .

We can set the Content-Type response header by writing setting the ctx.type property. For instance, we can do that as follows:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  ctx.type = 'text/plain; charset=utf-8';
  ctx.body = 'foo';
});

app.listen(3000);

In the code above, we have the following code:

ctx.type = 'text/plain; charset=utf-8';

to set the Content-Type header to ‘text/plain; charset=utf-8’ . Then we’ll see that as the value of the Content-Type header when we make the request to the / route.

To append one more header to the response, we can call the ctx.append to append more headers. It takes the key and value as its 2 arguments.

For instance, we can use it as follows:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  ctx.append('a', 1);
  ctx.body = 'foo';
});

app.listen(3000);

In the code above, we called the ctx.append method with the header key 'a' and the corresponding value of 1.

Then when we make a request to the / route, we’ll get the response header A with the value 1 appearing.

Conclusion

We can set response headers by calling the ctx.append method. To return a response body, we can set the ctx.body property with a value.

To set the status code of the response, we set the ctx.status property.

Categories
Koa Nodejs

Receiving Requests with Koa

Koa is a small framework that lets us create backend apps that run on the Node.js platform.

In this article, we’ll look at how to receive request data with Koa.

Receiving Headers

We can get the request headers of a request by using the ctx.request.header property.

For instance, we can get the headers as follows:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  ctx.body = ctx.request.headers;
});

app.listen(3000);

In the code above, we just assigned the ctx.request.headers object as the value of ctx.body so that we’ll see the request headers as the response body.

Then we should see something like the following when we make a request to the / route:

{
    "host": "craftygrouchycables--five-nine.repl.co",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36 Edg/80.0.361.62",
    "content-length": "90",
    "accept": "*/*",
    "accept-encoding": "gzip, deflate, br",
    "cache-control": "no-cache",
    "content-type": "application/json",
    "postman-token": "fd561c8c-6419-4939-87f5-dc989d5b9845",
    "x-forwarded-for": "35.191.2.193",
    "x-replit-user-id": "",
    "x-replit-user-name": "",
    "x-replit-user-roles": ""
}

We can also set the request.header to set the request header.

Its alias is request.headers . They get and set the same data.

Getting the Request Method from a Request

We can use the ctx.request.method to get the request method of the request.

For instance, we can display it on the screen by writing the following code:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  ctx.body = ctx.request.method;
});

app.listen(3000);

Like with the request.header property, we set the ctx.request.method as the value of ctx.body , then we see GET displayed on the screen if we make a GET request to the / route.

Get the Value of the Content-Length Request Header

We use the ctx.request.length to get the value of the Content-Length request header. If it’s present, it’ll return the value. Otherwise, undefined is returned.

Get the URL of the That’s Requested

The ctx.request.url property has the URL that’s requested by the HTTP request to our app.

We can display it as follows:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  ctx.body = ctx.request.url;
});

app.listen(3000);

We just set the url property to the ctx.body to return it as the response body. If we make a request to the / route, then we get / displayed.

It can also be used as a setter. Therefore, it makes this property handy for doing URL rewrites. For instance, we can rewrite requests to /foo and rewrite them to / by writing the following code:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  if (ctx.request.url.includes('/foo')){
    ctx.request.url = '/';
  }
  ctx.body = ctx.request.url;
});

app.listen(3000);

In the code above, we checked that if our request URL includes the /foo segment. If it does, then we change it to / .

Therefore, when we make a request to / or /foo , we’ll see / displayed on the screen.

Photo by Max Letek on Unsplash

Get the Original URL of the Request

We can use the ctx.request.originalUrl property to get the original request URL. It’ll be a relative URL. So if we make a request to /foo to a Koa app that has the following code:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  ctx.body = ctx.request.originalUrl;
});

app.listen(3000);

We’ll see that /foo is displayed.

If we want to include the protocol and host of the server in the URL, then we can use the ctx.request.origin property instead.

For instance, we can write the following code:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  ctx.body = ctx.request.origin
});

app.listen(3000);

Then we get the protocol and hostname of the server that our app resides in.

If we want to get the full URL, then we can use the ctx.request.href property.

For instance, we can write the following code:

const Koa = require('koa');
const app = new Koa();

app.use(async (ctx, next) => {
  ctx.body = ctx.request.href;
});

app.listen(3000);

Then when we make a request to https://CraftyGrouchyCables–five-nine.repl.co/foo, we get back the whole URL on the screen if we have the code above.

Conclusion

We can get various pieces of data from a URL with the request property of the ctx context object. It has the header, request URL, request method and more.