Can get('/', function (req, res){} can't be called server - node.js

I was working on fetching data from mysql and using it to create d3js graph on node.js. I learnt that server can only be made from modules 'http' and 'request'. If I am not using them and using
app.get('/', function (req, res) {
res.render('index', {
//
});
});
app.listen(port, function () {
console.log('running server on port ' + port)
});
They are solving the same purpose.
What concept of server am I missing? What are the limitations of fetching data this way?

use express web framework :
const express = require('express')
const app = express()
app.get('/', function (req, res) {
res.send('Hello World')
})
let port=3000;
app.listen(port, function () {
console.log('running server on port ' + port)
});

Related

My node.js server is up, but discord.js bot is offline

I'm using discord.js to make a discord bot on replit. I have a keep_alive which makes the server remain online with no problem, but the bot goes offline after a while.
Currently, uptimerobot is keeping the server alive, but the bot goes down in a few hours. I tried to following code to detect ratelimits:
client.on('debug', (a)=>{
if(a.startsWith(`Hit a 429`)){
process.kill(1)
}
});
client.on("rateLimit", data => {
process.kill(1)
})
client.on('rateLimited', () => {
process.kill(1);
});
And here is my code for the keep_alive:
var http = require('http');
http.createServer(function (req, res) {
res.write("I'm alive"); res.end();
}).listen(8080);
I've also tried using:
//https://github.com/mrmotchy/keep-alive
const express = require('express');
const server = express();
server.all('/', (req, res) => {
res.send(`OK`)
})
function keepAlive() {
server.listen(3000, () => { console.log("Server is Ready!!" + Date.now()) });
}
And
//https://replit.com/talk/learn/How-to-keep-your-discordant-bot-alive-in-Replit-2020/46432
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => res.send('Hello World!'));
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));
And also other websites similar to uptimerobot, all of these combinations yield the same result.

Expressjs server and external api calls

I'm new to frontend development and express server. When I tried to start an express.js server with react (with axios calls to external apis), it seems express.js is adding 'localhost:3000' in front of the external API calls so they fail.
In my server.js:
const path = require('path');
const express = require('express');
const app = express();
const publicPath = path.join(__dirname, '.', 'dist');
const port = process.env.PORT || 3000;
app.use(express.static(publicPath));
app.get('*', (req, res) => {
res.sendFile(path.join(publicPath, 'index.html'));
});
app.listen(port, () => {
console.log('Server is up!');
});
Which leads to the API call to www.example.com/api/ to become http://localhost:3000/www.example.com/api/
I also tried to filter the req by writing:
app.get('*', (req, res) => {
if (req.url.match(/\/api\//) === null) {
res.sendFile(path.join(publicPath, 'index.html'));
}
});
But it does not change things...
Can anyone help out this newbie that is me?
Update1 Adding the code for calling the api:
This is the api call:
const getSomething = () => {
try {
const url = endpoints.GET_SOMETHING;
return axios.get(url);
} catch (err) {
console.log(err);
}
};
endpoints.GET_SOMETHING is the api URL: www.example.com/api/getSomething
You need to put a / in the url
app.get('/*', (req, res) => {
res.sendFile(path.join(publicPath, 'index.html'));
});
and also your endpoint url should start with https://, http:// or //

I'm new with API's. I'm working with rest API's in node js. I'm not getting how to go from one route to another?

In the example below, if I'm in localhost:5000/A and i want to go to localhost:5000/B, but from within localhost:5000/A, how can i do that via a button ?
var express = require('express');
var app = express();
// Our first route
app.get('/', function (req, res) {
res.send('Hello Index!');
});
// Our A route
app.get('/A', function (req, res) {
res.send('Hello A!');
});
// Our B route
app.get('/B', function (req, res) {
res.send('Hello B!');
});
// Listen to port 5000
app.listen(5000, function () {
console.log('Dev app listening on port 5000!');
});
Just try this :
app.get('/A', function (req, res) {
res.send('Hello A! <button>Click me</button>');
});
Or you could serve an HTML page containing this code.
Hope this answers your question. If not please reach back to me.

App.js to redirect to a module

I am debugging into a NODE JS application and I am very new to node js. I have a REST module file
students.js
module.exports = function (service) {
/**
* Retrives data from DB
*/
service.get('/mobile/students', function (req, res) {
res.set('Content-Type', 'application/json')
.status(200)
.json(DBHelper.getAllStudents());
});
service.post('/mobile/students', function (req, res) {
res.status(200).json(data);
});
});
To run it locally I am using the following app.js
const express = require('express');
const app = express();
var routes = require('./students');
app.get('/', function (req, res) {
res.send('Hello World!')
});
app.listen(3010, function () {
console.log('Example app listening on port 3010!')
});
When I hit
http://localhost:3010/students, I am hitting a 404.
How do I explicit route the path to the student modules?
you need to add routes(app); line after var routes = require('./students'); then Your routes will be mounted..
http://localhost:3010/students if use this it will prompt you again with 404 but if you use http://localhost:3010/mobile/students it will produce desire output..

Multiple routes matched at the same time - Strange Express behavior

I am wondering if the following express behavior is valid :
In this example, making a GET request "GET /count" will trigger/execute both routes /count and /:mail.
var express = require('express')
var app = express()
app.get('/count', function (req, res) {
res.send('Hello World!1')
})
app.get('/:mail', function (req, res) {
res.send('Hello World!2')
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
When the /:mail is triggered, the output will be : "Error: Can't set headers after they are sent."
My question is : Isn't it obvious for express to match only one single route at once ?

Resources