justZito
justZito

Reputation: 559

What is the proper use case for readfilesync with node.js?

I have this code below where I can now read the raw sql files put them into the var of selectFrom by using readFileSync for a server side CLI type of exercise. The question I have, is it okay not to have a call back on server side coding or should it always be done the callback way not to block the IO. I know this is just a learning exercise but I want it to be right to grow my knowledge and pick up good habits.

var fs = require('fs');
var sqlFile = 'inventory.sql'
var sqlPath = './SQLs/'

var selectFrom = fs.readFileSync(sqlPath + sqlFile, 'utf8')

console.log(selectFrom)

Upvotes: 1

Views: 408

Answers (1)

Sergey Yarotskiy
Sergey Yarotskiy

Reputation: 507

You can use Sync function only while server loading. After it started use async mode. Even if for you this job can be done in sync mode, there still can be some parallel user contacting your server. While Sync functions WHOLE SERVER stop to response.

Upvotes: 1

Related Questions