Server is starting without any errors but the URL is hit its showing no application running.
URL:link_To_Server
My Code:
var express= require('express');
var app=express();
app.get('/', function(req, res, next) {
console.log('home');
res.send(200);
res.end('getting requests');
});
console.log('server started');
You didn't actually set the server to listen to a port. Replace
console.log('server started');
With
app.listen(3000, function() {
console.log('server started');
});
Note that with the above example, the bound port is 3000. For more info, see the API doc
Related
iam new in node.js .iam faceing this problem .When i use express and use ''use'' request to get response to the chorome but this erro came in my chorme bowser Cannot GET.
or my app.js file or index.js is in one same folder . folder name is static. Iam not good in english please help me. Or if i use nodemon togther than when i get to localhost the the loaclhost cannot and show this 'This site can’t be reached'
i dont know how to fix it can any help me to fix this problem this the code
const express = require("express");
const app = express();
const port = 8080;
app.use('/static',express.static('express/static'));
app.get("/", (req, res)=>{
res.status(200).send("This is the first page of node express");
});
app.get("/about", (req, res)=>{
res.send("This is the about page of node express");
});
app.post("/postabout", (req, res)=>{
res.send("This is the postabout page of node express");
});
app.post("/erro", (req, res)=>{
res.status(404).send("This page has been errored in code of 404");
});
app.listen(80,'127.0.0.1', () =>{
console.log(`The application started and the port is ${port}`);
});
This the image of my code and chrome
In app.listen you passed wrong port means you intialized port as 8080 but passed 80
app.listen(8080,() =>{
console.log(`The application started and the port is ${port}`);
});
Basically, app.use function uses a middleware when a particular API is called it does not a get request.
So try app.get instead of app.use it will work.
app.get('/static', (req, res) => {
// Do your stuffs.
return res.send(response)
})
I have a Node.js app that uses Express.js to listen for connections. The code is something like so:
const express = require("express");
var server = express();
server.get("/test", (req, res) => testResponse(req, res));
server.listen(9001);
console.info("Server is listening to port 9001.");
I'd like to implement a way to restart the server without having to restart the whole app. However, I can't seem to properly shut down the server and free the port.
Here's what I tried:
server.close();
console.info("Server closed. Restarting.");
var server = express();
server.get("/test", (req, res) => testResponse(req, res));
server.listen(9001);
console.info("Server is listening to port 9001.");
As soon as I run this, I get
Error: listen EADDRINUSE :::9001
What would be the correct way to do this?
Cheers.
server.close() is asynchronous and takes a callback. Try waiting until the server is actually closed before starting a new one:
server.close(()=>{
console.info("Server closed. Restarting.");
var server = express();
server.get("/test", (req, res) => testResponse(req, res));
server.listen(9001);
console.info("Server is listening to port 9001.");
});
As of Express 3, the app.close() method seems to have disappeared, which means Express users have no means of gracefully stopping an application. Express is really a listener to http request events which means you can do this:
const express = require("express");
var server = express();
server.get("/test", (req, res) => testResponse(req, res));
var app = server.listen(9001, function () {
console.log('Listening :)');
app.close(function () {
console.info("Server closed. Restarting.");
var server = express();
server.get("/test", (req, res) => testResponse(req, res));
server.listen(9001);
console.info("Server is listening to port 9001.");
});
});;
console.info("Server is listening to port 9001.");
For more on this you can refer hardening nodejs for production
I just got an existing project with react and node.js
When I run the react app the server call fails,
the React is on 8080,
the server looks something like this
// Listening on port 3000 for testing and 80 for prodution
app.listen(3000, function () {
winston.log('info', 'Listening on port 3000!');
});
// Index is currently inactive
app.get('/', function (req, res) {
res.send('');
});
// Call and load React
app.get('/app', function(req, res) {
res.sendFile(path.resolve(__dirname, 'public/index.html'));
});
// API for Web App
app.use('/api/v1', routes)
// API for Mobile App
app.use('/mapi/v1', mroutes)
// DEFINE ERROR BEHAVIOUR
// =============================================================================
// catch 404 and forward to error handler
app.use(function(req, res, next) {
res.status = 404;
res.sendFile(path.resolve(__dirname, 'public/index.html'));
// next(err);
});
this is how I got the project, I'm assuming it should work, I just need to make the node project somehow to run on port 8080(with the react app)
.
the client request is to localhost/8080(but changes depeneding on the port of the project),
if I'm runing react on 8081 the error will be "can't find localhost 8081/api/v1/GetUsers(example..)"
the means that the previous developer intended that the server and the client will be on the same port, but I don't how
For some reason I keep getting "http://127.0.0.1:3000/socket.io/socket.io.js" 404 not found when I look under chrome developer tools network. I have spent over an hour trying to understand why this isn't working because it was working fine before and I didn't think I changed anything.
Server:
var express = require('express'),
session = require('express-session');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var MongoStore = require('connect-mongo')(session);
var sessionMiddleware = session({
resave: false,
saveUninitialized: false,
secret: 'secret',
store: new MongoStore({url: "mongodb://localhost:27017/database"})
});
app.use(sessionMiddleware);
io.use(sessionMiddleware, function(socket, next) {
sessionMiddleware(socket.request, socket.request.res, next);
});
io.sockets.on('connection', function (socket) {
console.log("Socket connected");
});
app.set('view engine', 'pug');
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res) {
res.render('index');
});
app.listen(3000);
console.log('listening');
Pug File:
html
head
script(src="http://127.0.0.1:3000/socket.io/socket.io.js")
script(type='text/javascript', src='../javascripts/chat.js')
body
Client javascript:
try {
var socket = io.connect('http://127.0.0.1:3000');
} catch(e) {
console.log(e);
}
Change this:
app.listen(3000);
to:
server.listen(3000);
app.listen() creates a new and different server so the server that you have socket.io attached to is not the one that is actually listening and thus your socket.io server is not live and not able to handle the /socket.io/socket.io.js request or the incoming connection if the script file didn't first fail to load.
See this answer for more details on what app.listen() does and why it isn't what you want the way your code is laid out:
websockets, express.js and can’t establish a connection to the server
Note, you could use app.listen(), but you'd have to not create your own server and you'd have to capture the return value from app.listen() and use that as the server you pass to socket.io initialization:
var app = express();
var server = app.listen(3000);
var io = require('socket.io')(server);
Which is not quite how your code is structured (but you could rearrange things to do it this way).
I'm running the basic express server code(given below). I'm trying to get the req.params values through routes.
var express = require("express");
var app = express();
var PORT = process.env.PORT || 3000;
app.get("/", function(req, res, next){
res.send("Timestamp Microservice");
});
app.get("/:unix", function(req, res, next){
res.send(req.params);
});
app.listen(PORT, function(){
console.log("Server running on: " + PORT + "!");
});
on localhost:3000/1234, response is a JSON object
{ unix: "1234"}
but when I deploy this app to Heroku, it shows
Cannot GET/1234
Can you tell me whats the issue and how it can be resolved? I just started learning Node and Express and don't know much. Thank you.
Update:
I have to use body-parser node module and now the issue is resolved.
var bodyParser = require("body-parser");
app.use(bodyParser.json());