Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file_get_contents() equivalent for Node.JS

Tags:

I was wondering if there was any file_get_contents() equivalents in Node.JS modules or elsewhere. It has to lock the process until the download is finished, so the existing request() code in Node.js won't work. While it doesn't need to read into the string, the locking, synchronous nature is important.

If this doesn't exist, is using CURL via the OS module an efficient way of handling the same process?

like image 793
Kyle Hotchkiss Avatar asked Jan 08 '12 05:01

Kyle Hotchkiss


People also ask

CAN node JS run without nginx?

So, in short: You dont need Nginx or Apache at all, but you can use if you want. It's very cosy to some people use Nginx to do the load balance, or even other stuff like handle the https or server static content. It's your choice at the end.

Can we write node js test without external library?

If you've ever written tests for a Node. js application, chances are you used an external library. However, you don't need a library to run unit tests in Javascript.

What is test in Nodejs?

Unit Testing is a software testing method where individual units/components are tested in isolation. A unit can be described as the smallest testable part of code in an application. Unit testing is generally carried out by developers during the development phase of an application. In Node.


1 Answers

fs.readFileSync appears to do what you're asking. From the manual:

fs.readFileSync(filename, [options])

Synchronous version of fs.readFile. Returns the contents of the filename.

If the encoding option is specified then this function returns a string. Otherwise it returns a buffer.

like image 80
WildlyInaccurate Avatar answered Sep 20 '22 09:09

WildlyInaccurate