Questions about Node.js

greetings, I will like to have general idea about node.js and server.
Like client side we fetch or use index.html elements in script.js as (object) . With the aid of document.querySelector etc. Which I believe server.js (node) are using require ().
But my question is where are they fetching or aquiring their object from, because I can see express coming from npm. so am confuse though I want to have the general idea, Help thanks.

Hi @ychris !

I moved this post to its own topic since it was off topic from the previous conversation.

I wish I could give you an in depth answer but I only have a small amount of
experience working with node so maybe someone else on the forum can assist you better.

Good luck!

3 Likes

JavaScript can be so confusing with all its variants and different ways of configuring. Hopefully I can help make some of it make a bit of sense.

First, imports. When you write a bit of code in a file and then import that code from another file, the code you are importing is called a “module”.

NodeJS used to only support importing modules using the require() syntax (refered to as “Common JS”) but later versions of NodeJS let you import modules with the import syntax. For example import https from 'https' will import the included https module.

That style of importing modules is called “ES6 Imports” and modules using the style are called “ES Modules”. You can actually use that style in older versions of Node too if you use a transpiler like Babel.

Now for the runtime. NodeJS is a runtime which allows you to use JavaScript as a server-side language but also means a lot of functionality you’re used to having client side (in the browser) doesn’t exist. There is no DOM in NodeJS so anything you know associated with document.anything is no longer available or would even make sense in the context of running server side.

Things like local storage and XHR requests are all gone too. Basically anything you can think of that would only make sense in the browser is gone.

As for fetching data over a network, you might be familiar with fetch() which you can use when running JavaScript in the browser. NodeJS instead has https but a lot of people just use npm to import axios instead.

At the end of the day server-side and client-side JavaScript are the same language. The biggest difference is the built-in API’s that are available to you and you get familiar with the difference in time.

Hope that helps at least a little!

3 Likes

Thanks, so helpful and I appreciate.

1 Like

Please help on this one. After deleting the link from view that reference public , I now added this code

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

app.get("/", (req, res) => res.sendFile(__dirname + 
"/views/index.html")
);

app.use(express.static(__dirname +
"/public/style.css"));








































 module.exports = app;
But still cant pass the challenge 

Please create a new topic for this new challenge. This post is off topic from this current discussion.
You should also include the challenge link so people will know what challenge you are working on.

Edit: I see that you have created a new topic but I would still advise you not to post here as well since it is still off topic.

2 Likes

Greetings, I have just open up an issue and paste the code . But the major problem is that, I have serve views, I equally serve public as instructed , I Google the samples , but the public is not working .what must I do