Categories
JavaScript Answers

How to set uploaded file and folder permissions with AWS S3 Node.js SDK?

Sometimes, we want to set uploaded file and folder permissions with AWS S3 Node.js SDK.

In this article, we’ll look at how to set uploaded file and folder permissions with AWS S3 Node.js SDK.

How to set uploaded file and folder permissions with AWS S3 Node.js SDK?

To set uploaded file and folder permissions with AWS S3 Node.js SDK, we can set the ACL property of the object that we call putObject with.

For instance, we write

const params = {
  Bucket: bucketName + path,
  Key: key,
  Body: buffer,
  ContentEncoding: 'base64',
  ContentType: 'image/jpeg',
  ACL: 'public-read'
};
await s3.putObject(params).promise();

to call putObject with params that has ACL set to 'public-read' to let anyone read the uploaded file.

Conclusion

To set uploaded file and folder permissions with AWS S3 Node.js SDK, we can set the ACL property of the object that we call putObject with.

Categories
JavaScript Answers

How to use a regex for route matching in Express and Node.js?

Sometimes, we want to use a regex for route matching in Express and Node.js

In this article, we’ll look at how to use a regex for route matching in Express and Node.js.

How to use a regex for route matching in Express and Node.js?

To use a regex for route matching in Express and Node.js, we can use a regular JavaScript regex as route path argument.

For instance, we write

app.get(/^\/(discussion|page)\/(.+)/, (req, res, next) => {
  //...
});

to call app.get with a regex that matches discussion or page with any characters after either one.

Conclusion

To use a regex for route matching in Express and Node.js, we can use a regular JavaScript regex as route path argument.

Categories
JavaScript Answers

How to separate routes on Node.js and Express 4?

Sometimes, we want to separate routes on Node.js and Express 4.

In this article, we’ll look at how to separate routes on Node.js and Express 4.

How to separate routes on Node.js and Express 4?

To separate routes on Node.js and Express 4, we can put our routes into a separate and call require to add them.

For instance, we write

app.js

const express = require('express');
const app = express();

app.use(require('./routes'));

const server = app.listen(8000, () => {
  const host = server.address().address
  const port = server.address().port
  console.log("listening at http://%s:%s", host, port)
})

to call app.use with require('./routes') to add the routes in routes.js.

Then in routes.js, we write

const express = require('express');
const router = express.Router();

router.use((req, res, next) => {
  next();
});

router.get('/', (req, res) => {
  res.send('home page');
});

router.get('/about', (req, res) => {
  res.send('About us');
});


module.exports = router;

to create a router object with express.Router.

Then we call router.use and router.get to add the middleware and add GET endpoints.

Next, we set module.exports to router to export router so we can use require('./routes') to include the router and call app.use to add the routes.

Conclusion

To separate routes on Node.js and Express 4, we can put our routes into a separate and call require to add them.

Categories
JavaScript Answers

How to run multiple statements in one query with node-mysql?

Sometimes, we want to run multiple statements in one query with node-mysql.

In this article, we’ll look at how to run multiple statements in one query with node-mysql.

How to run multiple statements in one query with node-mysql?

To run multiple statements in one query with node-mysql, we can call connection.query with all our SQL statements.

For instance, we write

connection.query('SELECT ?; SELECT ?', [1, 2], (err, results) => {
  if (err) {
    throw err;
  }

  const [r1, r2] = results
  console.log(r1);
  console.log(r2);
});

to call query with 2 SELECT statements separated by a semicolon.

And then we pass in an array the values for the ? in the SQL code.

Next, we pass in a callback that has the SELECT query results stored in the results parameter.

As a result, r1 is [{1: 1}] and r2 is [{2: 2}].

Conclusion

To run multiple statements in one query with node-mysql, we can call connection.query with all our SQL statements.

Categories
JavaScript Answers

How to set navigation timeout with Node.js Puppeteer?

Sometimes, we want to set navigation timeout with Node.js Puppeteer.

In this article, we’ll look at how to set navigation timeout with Node.js Puppeteer.

How to set navigation timeout with Node.js Puppeteer?

To set navigation timeout with Node.js Puppeteer, we can call goto with the timeout option.

For instance, we write

await page.goto(url, {
  waitUntil: 'load',
  timeout: 0
});

to call goto with the url to navigate to the URL.

And we call it with an object that has timeout set to 0 to set the navigation timeout to 0ms.

Conclusion

To set navigation timeout with Node.js Puppeteer, we can call goto with the timeout option.