-2

I'm trying to execute a simple express script in javascript. Everytime I try I receive an error of "require is not defined". code snippets below... Thanks!

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

app.get('/', (req, res) => {
    res.send('An alligator approaches!');
});

app.listen(3000, () => console.log('Gator app listening on port 3000!'));

and the index.html file looks like such...

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <script src="basic.js"></script>
</head>
<body>

</body>
</html>
4
  • You need to run it from the command like node app.js, then go to the browser localhost:3000 Commented May 11, 2018 at 20:51
  • Is that first JS snippet "basic.js"? Commented May 11, 2018 at 20:52
  • Have you installed nodejs ? Commented May 11, 2018 at 20:52
  • Express is a web server framework. What's your goal in trying to run it inside a web browser? Commented May 11, 2018 at 20:52

1 Answer 1

1

Hi as you may know NodeJS is a server side language so it cannot be understand by your browser.

Actually you have created an express server using NodeJS, so if you want to execute your server just run "node basic.js" in your terminal.

Sign up to request clarification or add additional context in comments.

1 Comment

I wasn't aware I needed to execute express via the command line. I was trying to execute via browser. Thanks for the quick response!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.