I just started learning nodejs and there is some weird error coming when I try to run nodemon server.js command.
Here is my server.js
const express = require("express");
const app = express();
const server = require("http").Server(app);
app.get("/", (req, res) => {
res.status(200).send("Hello World");
});
server.listen(3030);
My VScode terminal shows this but the server never starts.
Here is package.json
{
"name": "video-chat-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"ejs": "^3.1.6",
"express": "^4.17.1",
"peer": "^0.6.1",
"socket.io": "^4.1.2",
"uuid": "^8.3.2"
},
"devDependencies": {
"nodemon": "^2.0.7"
}
}
You don't need to use http, express is already enough to start a server.
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.status(200).send("Hello World");
});
app.listen(3030, ()=>{
console.log('Server is starting');
});
Related
I'm trying to upload a node app to Vercel and use as API but I'm getting an This Serverless Function has crashed. error message. The fact that I can run it with no problems in localhost, and the fact that the build log doesn't throw an error, I can't seems to find the problem.
Here is a full Screenshot:
vercel app
And here is my index.js:
const express = require(`express`);
var cors = require('cors')
const app = express();
app.use(cors())
const bodyParser = require('body-parser')
const mongoose = require("mongoose")
require("dotenv").config();
const config = require('config');
const dbConfig = config.get("MT.dbConfig.dbName")
mongoose.connect(dbConfig, {
}).then(() => {
console.log('Database connected.')
}).catch((err)=> console.log('Something went wrong with the database : ' + err))
mongoose.Promise = global.Promise;
app.use(bodyParser.json())
app.use('/', require('./api/v1/api'))
app.use((err, req, res, next) =>{
res.status(422).send({
error: err._message
})
})
const PORT = 5001;
app.listen(PORT, () => console.log("API is running."))
And here is my package.json:
{
"name": "hidden",
"version": "1.0.0",
"description": "",
"main": "index.js",
"engines": {
"node": "14.x"
},
"scripts": {
"start": "vercel dev",
"deploy": "vercel deploy --prod"
},
"author": "hidden",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"config": "^3.3.6",
"cors": "^2.8.5",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"mongoose": "^6.0.13",
"vercel": "^23.1.2"
},
"devDependencies": {
"nodemon": "^2.0.15"
}
}
I'm using Windows and I've just installed nodemon 2.0.12 (added to path). Whenever I run a basic application it works it should until I save a file, then I receive the following error
My project is a basic express app:
const express = require('express')
const app = express()
const PORT = 3000
app.use("/", (req, res) => res.send('test'))
app.listen(PORT, function() {
console.log('Server started on port 3000')
})
My package.json is as follows
{
"name": "express.1",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"dev": "nodemon app.j"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"nodemon": "^2.0.12"
}
}
What might be the reason for this?
Downgrading Nodemon to v2.0.7 seems to work. I don't understand why it broke in the first place.
I installed nodemon.
I made changes to my code.
It is stuck at RESTARTING DUE TO CHANGES and doesn't restart the server.
Why is this happening and how to solve it?
>>index.js
const express = require('express');
const app = express();
app.get('', (req, res) => {
res.send('Hello, world!');
})
app.get('./help', (req, res) => {
res.send('Help Page!');
})
const port = 3000;
// const hostName = '127.0.0.1';
app.listen(port, () => {
console.log(`Server is listening on ${port}`);
});
>>package.json
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon index.js"
},
"author": "Sharjeel",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"nodemon": "^2.0.12"
}
}
The problem is with the latest version of the nodemon. The previous or older version runs completely fine without making any issue i.e. nodemon#2.0.7.
I'm trying to use nodemon on my macbook and whenever i run
nodemon <script.js>
It gives me this error:
address already in use <PORT NUMBER HERE>
Even if i'm running the script for the very first time. I've tried
npx kill-port <port>
But it doesn't work: it shows that a process was killed but when i try to run nodemon again, i get the same error.
Here's my package.json:
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "^10.0.0",
"express": "^4.17.1",
"nodemon": "^2.0.7"
}
}
My server.js:
const express = require("express");
const dotenv = require("dotenv");
const app = express();
dotenv.config();
const PORT = process.env.PORT;
app.get("/getRestaurants", (req, res) => {
console.log("Get All Restaurants");
});
app.listen(PORT, () => {
console.log(`Server is up on port ${PORT}`);
});
And my .env:
PORT = 4000;
What could be causing this? I'm on a M1 MBA with bigsur.
I am trying to use ES6 Modules to import Express. Although I added "type":"module" in my package.json. The error, SyntaxError: Cannot use import statement outside a module, still occurs. I am expecting an answer which does not require to convert into a .ejs extension since wanting to know what's wrong in my code rather than taking an alternative. Note: package.json & server.js are in the same directory.
package.json
{
"name": "",
"version": "1.0.0",
"description": "",
"main": "server.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"mongoose": "^5.10.3",
"nodemon": "^2.0.4"
}
}
Server.js
import express from 'express';
const app = express()
const port = process.env.PORT || 9000
app.get('/', (req, res) => {
res.status(200).send('hello world');
})
app.listen(port, () => {
console.log(`Listening on localhost: ${port}`)
});