I am creating a web server that will allow users make a request for fortunes, and get the data which I specified in my fortunes.json file. I am using express in node to do this.
I start my server this way npm run dev, however when I do so, I get the following error:
SyntaxError: Unexpected token const
at Module._compile (internal/modules/cjs/loader.js:720:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:643:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:839:10)
at internal/main/run_main_module.js:17:11
Does anybody have an idea what I am doing wrong, my syntax, and packages.json file, etc. all seems to be fine but I am still getting this.
I am expecting to see "server listening on port 3000" when I run the server however I get a token error.
const express = require('express');
const port = 3000,
const app = express();
app.get('/fortunes', (req,res) => {
console.log('requesting fortunes');
});
app.listen(port, () => console.log('Listening on port ${port}'));
You have two issues in your code.
1 . when you are initializing variable:
As you have put ',' (comma) thus you cannot specify its declaration again.
your code
const port = 3000,
const app = express();
Do like this :
const port = 3000,
app = express();
or use this :
const port = 3000;
const app = express();
When using string template use `(backtick).
app.listen(port, () => console.log(`Listening on port ${port}`));
Try below Code:
const express = require('express');
const port = 3000,
app = express();
app.get('/fortunes', (req,res) => {
console.log('requesting fortunes');
});
app.listen(port, () => console.log(`Listening on port ${port}`));
In ECMAScript 2015, you can use backtic as follow in your code:
app.listen(port, () => console.log(`Listening on port ${port}`));
or use :
app.listen(3000, () => console.log('Connected to the port'));
Related
I'm trying to upload my NodeJS express server to cdnapi.club through CPanel, It is using v16.17.1 and has DiscordJS, Express.
This is the error I am currently getting
Listening on port 3000
stderr:
/home/cdncraft/nodevenv/cdnapi.club/16/lib/node_modules/undici/lib/client.js:1184
if (socket[kParser].timeoutType !== TIMEOUT_IDLE) {
^
TypeError: Cannot read properties of undefined (reading 'timeoutType')
at _resume (/home/cdncraft/nodevenv/cdnapi.club/16/lib/node_modules/undici/lib/client.js:1184:29)
at resume (/home/cdncraft/nodevenv/cdnapi.club/16/lib/node_modules/undici/lib/client.js:1148:3)
at connect (/home/cdncraft/nodevenv/cdnapi.club/16/lib/node_modules/undici/lib/client.js:1133:3)
It starts listening to the actual port and then it crashes for some reason. I have been looking and trying to fix things for hours and haven't seen a fix. This is the index.js file for starting the server
const express = require('express');
const app = express();
const port = 3000
app.set('port', port)
const v1_path = "/api/v1"
app.listen(port, () => {
console.log(`Listening on port ${port}`)
})
app.get(`/`, (request, response) => {
response.sendFile(__dirname + '/pages/index.html')
})
This is the package.json https://pastes.dev/aUcTtdU0vX
I have centos 6 running nodejs 10,
and i have this scaffold code to test my application server but still giving Error: connect ECONNREFUSED ip:4000
const express = require('express')
const app = express()
const port = 3432
const https = require('https');
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
When i try to put the url in the browser it says This site can’t be reached.
I like server is not running but it is running.
The error was that server administrator dont enable the port for me
terminal after trying to run server server
const express = require('express')
const colors = require('colors')
const dotenv = require('dotenv').config()
const {errorHandler} = require('./middleware/errorMiddleware')
const connectDB = require('./config/db')
const port = process.env.PORT || 5000
connectDB()
const app = express()
app.use(express.json())
app.use(express.urlencoded({extended: false}))
app.use('/api/goals', require('./routes/goalRoutes') )
app.use(errorHandler)
app.listen(port, () => console.log(`Server started on port ${port} `) )
There is already an application listening on port 5000, like the error message suggests. Close any other command prompt/bash that may be running an instance of the application and try again.
Port 5000 is already in use by another application. Either close this port or choose an unused port by:
Changing: const port = process.env.PORT || 5000 to const port = process.env.PORT || <otherport>
or by providing an unused port as an environmental variable.
I am trying to deploy my full stack application built using MySQL, Express, Angular and Node on Plesk Obsidian. However, I am facing an issue during the deployment. When my client side sends a request to the server then, I get the following error:
"Http failure response for https://example.com:3000/api/mytargetapi: 0 Unknown Error"
"GET ... net::ERR_CONNECTION_REFUSED"
Everything was working smoothly until I deployed it on Plesk.
Here's my code of the request on the client side:
const params = new HttpParams().set('id', this.searchInDirectory);
this.http.get('https://example.com:3000/api/mytargetapi', {params})
.subscribe(response =>
{
this.itemDisplay = response;
}
server side:
var app = express();
app.disable('x-powered-by');
app.use(bodyParser.json());
app.use(cors());
app.use(routes);
const port = process.env.PORT || 3000;
const host = 'localhost';
const server = http.createServer(app);
server.listen(port, host, () => {
console.log(`Running on port ${port}`);
});
router.get('/api/mytargetapi', (req, res) => {
stuff here...
}
Does anybody know what am I doing wrong? I have searched the same question on stack overflow and followed each and every individual's solution but nothing seems to be working for me. Any help would be appreciated. Thanks!
I don't know how you're organizing your project structure for the back-end API, but you should test the API via POSTMAN before integration with your front-end client
var app = express();
app.disable('x-powered-by');
app.use(bodyParser.json());
app.use(cors());
app.get('/api/mytargetapi', (req, res) => {
stuff here...
}
app.use(routes);
const port = process.env.PORT || 3000;
const host = 'localhost';
const server = http.createServer(app);
server.listen(port, host, () => {
console.log(`Running on port ${port}`);
});
I have been struggling to find the source of an error in the following code:
const express = require("express");
const path = require("path");
const WebSocket = require("ws");
const SocketServer = WebSocket.Server;
const PORT = process.env.PORT || 3000; // port to listen on
const INDEX = path.join(__dirname, "index.html"); // index address
var server = express();
server.use((req, res) => res.sendFile(INDEX));
server.listen(PORT, () => console.log(`Listening on port ${ PORT }`));
const wss = new SocketServer({server}); // wss = web socket server
(I have code below for connection etc. that is irrelevant to this question I think)
Upon running this, I receive the following error from the client in the browser:
WebSocket connection to 'ws://localhost:3000/' failed: Connection closed before receiving a handshake response
What is confusing me is that the code works if I make the following change:
var server = express()
.use((req, res) => res.sendFile(INDEX));
.listen(PORT, () => console.log(`Listening on port ${ PORT }`));
The problem with this method is that it does not work to add in another .use before the existing .use, which is necessary for accessing static files (i.e. javascript as its own file instead of in an HTML file)
Why does changing my code between these two examples break it? What can I do to resolve this?
Thank you
You need to pass the http.createServer instance to the WebsocketServer. Not express instance
var app = express();
var server = http.createServer(app);
I found a solution!
It turns out that you must set the value of the server variable when .use and .listen are called.
var server = express();
server = server.use((req, res) => res.sendFile(INDEX));
server = server.listen(PORT, () => console.log(`Listening on port ${ PORT }`));
instead of
var server = express();
server.use((req, res) => res.sendFile(INDEX));
server.listen(PORT, () => console.log(`Listening on port ${ PORT }`));