Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
How can i get response from another JavaScript file while working with nodejs and express?

I'm trying to learn nodejs and express and i created a simple server. I want to run some JS code for response.

When I used this method it's works.

const express = require('express');
const path = require('path');
require('dotenv').config();


const app = express();
const port = process.env.PORT || "8000";



app.get('/', (req, res) => {
    res.send(`<script>
    console.log("Program works!");
    </script>`);
});



app.listen(port, () => {
    console.log(`Listening to requests on http://localhost:${port}`);
});

But writing JS as String is hard so I tried this:

const express = require('express');
const path = require('path');
require('dotenv').config();


const app = express();
const port = process.env.PORT || "8000";



app.get('/', (req, res) => {
    res.send(`<script src="./response.js"></script>`);
});



app.listen(port, () => {
    console.log(`Listening to requests on http://localhost:${port}`);
});

And i get this error:

GET http://localhost:8000/response.js net::ERR_ABORTED 404 (Not Found)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

When you send this:

<script src="./response.js"></script>

to the browser, the browser will parse that and see the src attribute and will then immediately request ./response.js from your server. But your server doesn't have any route to respond to that request (thus it gets a 404 error back from your server). Remember that a nodejs server serves NO files by default (unlike some other web servers). So, you have to create routes or middleware for anything that you want it to serve.

So, you need to add a route to your server that will response to a request for response.js. First change your <script> tag to this:

<script src="/response.js"></script>

You want the request to be "path absolute" so it does not depend upon the path of the containing page. Then, you need to add a route handler for response.js to your server.

This can be done as a single route:

app.get('/response.js', (req, res) => {
    res.sendFile("response.js", {root: __dirname});
});

Or, you can use express.static() to serve a whole directory of publicly available files with one line of code (if you also move all publicly available static files to their own directory away from your server files).

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!