screenshot of terminal
When running grunt serve the process gets stuck at express:dev task
while mongod is running and showing
> Running "express:dev" (express) task
> Starting background Express server
> Debugger listening on port 5859
Mongod response:
2016-12-24T19:50:20.085+0530 I NETWORK [initandlisten] waiting for connections on port 27017
2016-12-24T19:50:36.539+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:36894 #1 (1 connection now open)
2016-12-24T19:50:36.572+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:36896 #2 (2 connections now open)
My server app.js file is hereby and i don't know why grunt further task are executing is it something related to port please help ?
[/**
* Main application file
*/
'use strict';
// Set default node environment to development
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
var express = require('express');
var mongoose = require('mongoose');
var config = require('./config/environment');
// Connect to database
mongoose.connect(config.mongo.uri, config.mongo.options);
// Populate DB with sample data
// if(config.seedDB) { require('./config/seed'); }
// Setup server
var app = express();
var server = require('http').createServer(app);
var socketio = require('socket.io')(server, {
serveClient: (config.env === 'production') ? false : true,
path: '/socket.io-client'
});
require('./config/socketio')(socketio);
require('./config/express')(app);
require('./routes')(app);
// Start server
server.listen(config.port, config.ip, function () {
//console.log('Express server listening on %d, in %s mode', config.port, app.get('env'));
});
// Expose app
exports = module.exports = app;][1]
Related
Hi I am trying to connect MongoDB and I got an error. I worked on connecting DB by "connect with the MongoDB shell", but this time I want to connect with the "connect your application" option.
When I hit mongosh in the embedded terminal in my mac, below was returned.
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.2.2
Using MongoDB: 5.0.6
Using Mongosh: 1.2.2
...
...
...
test>
Because I am new to MongoDB, I don't even know if it's correctly working or not. Also, I wanna connect by coding. That's why I am asking here. Below are some parts of my code in an app I have been working on.
Thanks for your time for dedication here. So appreciate it.
// this is db.js file in a config folder.
const mongoose = require('mongoose')
// It must be a promise function to connect db
const connectDB = async () => {
try {
console.log(`try`)
const conn = await mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
dbName: 'expense2'
});
console.log(`MongoDB Connected: ${conn.connection.host}`.syan.underline.bold)
} catch (error) {
console.log(`Error: ${error.message}`.red)
process.exit(1);
}
}
module.exports = connectDB;
/*
Here is the error happened
Error: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
[nodemon] app crashed - waiting for file changes before starting...
*/
config.env in the config folder as well.
NODE_ENV = development;
PORT=5000
MONGO_URI=mongodb+srv://mongo:mongo#cluster0.8tjjn.mongodb.net/expense2?retryWrites=true&w=majority
// server.js file
const express = require('express');
const dotenv = require('dotenv');
const colors = require('colors');
const morgan = require('colors');
const connectDB = require('./config/db')
dotenv.config({ path: "./config/config.env" })
connectDB();
const app = express();
const transactionsRouter = require('./routes/transactions')
app.use('/transactions', transactionsRouter)
const PORT = process.env.PORT || 5000;
app.listen(PORT, console.log(`server running in ${process.env.NODE_ENV} mode on port ${PORT}`.yellow.bold));
I'm trying to deploy my Node.JS app to Azure App Service. I followed this introduction: https://learn.microsoft.com/de-de/azure/app-service/app-service-web-get-started-nodejs.
Here is the code of my app:
var express = require('express'); // Web Framework
var app = express();
var sql = require('mssql'); // MS Sql Server client
const { request } = require('http');
// Connection string parameters.
var sqlConfig = {
user: 'username',
password: 'password',
server: 'serveraddress',
database: 'databasename'
}
// Start server and listen on http://localhost:80/
var server = app.listen(80, function () {
var host = server.address().address
var port = server.address().port
console.log("app listening at http://%s:%s", host, port)
});
app.get('/tags', function (req, res) {
sql.connect(sqlConfig, function() {
var request = new sql.Request();
request.query('select * from dbo.Tag', function(err, recordset) {
if(err) console.log(err);
res.end(JSON.stringify(recordset)); // Result in JSON format
});
});
})
The app runs locally without any problems. Simple testing in browser by typing localhost:80/tags returns all tags as json.
But after deployment to Azure this error occurs:
2020-06-25T17:11:58.055Z ERROR - Container wearxapplication_0_ed215082 for site wearapplication did not start within expected time limit. Elapsed time = 230.0801107 sec
2020-06-25T17:11:58.074Z ERROR - Container wearxapplication_0_ed215082 didn't respond to HTTP pings on port: 8080, failing site start. See container logs for debugging.
2020-06-25T17:11:58.088Z INFO - Stopping site wearxapplication because it failed during startup.
What its mean? How solve it?
Looking at the errors, I believe you are using App Service on Linux with Single container configuration. If so, do not explicitly listen on port 80 or any other (unless you are deploying via custom container where you would have control of the docker file). Behind the scene, app service on Linux deploys a container and expose an auto-detected port (docker expose). Your explicit listening port is unlikely to match that auto-detected port. Replace you server bootstrap code with below snippet to let the port get picked up from environment:
// Start server and listen on http://localhost:port/ for local
const port = process.env.PORT || 1337; // for local debugging choose any available port
var server = app.listen(port, function () {
var host = server.address().address;
console.log("app listening at http://%s:%s", host, port);
});
One more thing to check is whether any of the dependencies (express, mssql in your case) failed to resolve at startup. This can happen when you have the dependency missing in node_modules folder. You can check that by starting Log Stream (you can find in Azure portal app service blade). You might see error like this
Log stream
I wrote a simple node express server for webRTC using peerjs-server and simple client using peerjs. Everything works fine on localhost, but when I try it on vps, I get error:
Firefox can't connect with server ws://my.vps/peerjs/peerjs?key=peerjs&id=hj3hpekwaa38fr00&token=ymtfvhagiw
PeerJS: Socket closed.
PeerJS: ERROR Error: Lost connection to server.
Error: "Lost connection to server."
emitError https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.16/peer.min.js:1:16426
_initializeServerConnection https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.16/peer.min.js:1:12260
emit https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.16/peer.min.js:1:25516
onclose https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.16/peer.min.js:1:19350
Server:
const express = require('express');
enter code here`const app = express();
const ExpressPeerServer = require('peer').ExpressPeerServer;
app.use(express.static('./public'));
const server = app.listen(80, () => { // 3000 on localhost
console.log('Express server listen on port ' + 80);
});
const options = { debug: true };
const peerserver = ExpressPeerServer(server, options);
app.use('/peerjs', peerserver);
app.use('/*', express.static('./public/index.html'));
Client:
var peer = new Peer('', {
host: location.hostname,
port: location.port || (location.protocol === 'https:' ? 443 : 80),
path: '/peerjs',
debug: 3
});
peer.on('open', function (id) {
console.log(id);
});
Any help appreciate.
It looks like you are connecting with server ws://my.vps/, which is a web socket to a server at http://my.vps/ which doesn't seem to exist.
It should probably also be using https (or wss)
I am trying to deploy a node app into AWS EC2 Ubuntu instance, but I am getting issues. It seems like for some reason it is only listening to port 22 (the default portal for SSH), and not my HTTP port.
netstat -ntlp | grep LISTEN
AWS Security Group Settings
As you can see in the link above, I did manually include port 80 as well as others into my security group. I don't think this is a firewall issue from what I have read.
My Express server code:
const express = require('express'),
app = express(),
bodyParser = require('body-parser'),
cookieSession = require('cookie-session'),
port = 8000,
mongoose = require('mongoose'),
mongoDB = require('./keys.js').database,
passport = require('passport'),
passportSetup = require('./passportConfig'),
keys = require('./keys'),
bluebird = require('bluebird');
mongoose.Promise = bluebird;
app.listen(port, () => {
mongoose.connect(mongoDB)
.then(() => {
console.log('successfully connected to MongoDB. Listening to
port ', port);
})
.catch(() => {
console.log('error connecting to MongoDB.');
});
});
I am able to actually access my Node server, but connection is refused in the browser.
NPM Start
Anyone have any ideas?
My app in nodeJS refuses to connect to the database when I run it, I am using mongoose ODM to connect to the database. When I run the application I get the error below.
Server running at port 8000
/Users/user/www/document-manager/node_modules/mongodb/lib/server.js:242
process.nextTick(function() { throw err; })
^
Error: connect ECONNREFUSED 127.0.0.1:27017
at Object.exports._errnoException (util.js:890:11)
at exports._exceptionWithHostPort (util.js:913:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1065:14)
Below is the code which I run:
var express = require('express'),
mongoose = require('mongoose'),
dotenv = require('dotenv'),
bodyParser = require('body-parser'),
router = require("./server/router");
//Initialize App
var app = express();
app.use(bodyParser.json());
//Initialize database connection
mongoose.connect("mongodb://localhost/dms");
router(app);
//Load Dotenv
dotenv.load();
//Set Port
var port = process.env.PORT || 8080;
//Run applicatino
app.listen(port, function(){
console.log("Server running at port " + port);
});
The application would run if I run mongod from the terminal separately. Is there a way to run my database server and my application server together?