bimlas
bimlas

Reputation: 2607

What is Preact exactly? Can I use Node modules?

I'm still new to the world of Node frameworks. I chose Preact because it seemed to have less dependency than React.

I think I originally misunderstood what exactly this is. I thought it works like PHP: we do tasks on the server page and the client gets the result in HTML form. I understand this is a framework that does not require a Node.js server, but when I create a project using Preact-CLI I can only run it on one Node server (at least opening the generated HTML file does not work by itself, the page does not respond when buttons are pressed, even if I change the file paths from absolute to relative).

I would like to read files in the Preact app, run MySQL queries, but these require Node modules. If const fs = require('fs'); is included in the Preact component, npm start will drop this error:

Module not found: Error: Can't resolve 'fs' in '...'

How do I get Node modules to work, read files, etc.? Should I use AJAX with a separate server (with completely separate code) and communicate with Preact?

Upvotes: 2

Views: 559

Answers (2)

Ruckert Solutions
Ruckert Solutions

Reputation: 1301

Node frameworks => You mean JS Frameworks :)

Preact is a library for the browser, not server, and as such does not have access to the filesystem and other stuff.

To read e.g. from a SQL database, you have to make a call to an API that queries the database and sends the result back to the client (browser).

Upvotes: 1

Alex Catchpole
Alex Catchpole

Reputation: 7336

Preact is a clientside framework like React, it's not a server side rendered framework.

You'll need to make api calls to an external server from your Preact app.

Upvotes: 0

Related Questions