I was trying to run express node.js to "localhost:3000" but it always give me an error on my hyper terminal "unexpected token "="
And here is the code
//JS hint esversion:6
const = require("express");
const app = express();
app.get("/", function(request , response) {
response.send("hello");
});
app.listen(3000, function() {
console.log("server started on prt 3000");
});
So whenever I run localhost:3000 the browser says This site can’t be reached
You must add the variable name after const
const express = require('express')
I think u have 2 add bodyparser.
Related
I am following a tutorial (https://levelup.gitconnected.com/simple-application-with-angular-6-node-js-express-2873304fff0f) on creating an app with Angula CLI, Node.js and Express. I use a proxy to start the app, the file defining the proxy looks like this:
{
"/api/*": {
"target": "http://localhost:3000",
"secure": false,
"logLevel": "debug",
"changeOrigin": true
}
}
The command I use to start the app is this one:
ng serve --proxy-config proxy.conf.json
The tutorial said that:
All requests made to /api/... from within our application will be forwarded to http://localhost:3000/api/...
To be honest, I don't really know how it is supposed to work, because when I launch the app, I still use the URL: http://localhost:4200/ .
But I didn't have a problem until now. I just created a route with Express.js at the Endpoint /api/v1/generate_uid .
But the problem is when I go to http://localhost:4200/api/v1/generate_uid it shows this message:
Error occured while trying to proxy to: localhost:4200/api/v1/generate_uid .
The following is the message I get in the console:
[HPM] Error occurred while trying to proxy request /api/v1/generate_uid from localhost:4200 to http://localhost:3000 (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)
And when I go to http://localhost:3000 it always says that the connection has failed.
For further references, Here are the app.js of my express API and generate_uid.js which defines the route:
app.js
var express = require('express');
var uid = require('uid-safe');
var router = express.Router();
router.get('/', function(req, res, next) {
var strUid = uid.sync(18);
res.json({guid: strUid});
});
module.exports = router;
generate_uid.js
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var users = require('./routes/users');
var generate_uid = require('./routes/generate_uid');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser())
app.use('/api/v1/users', users);
app.use('/api/v1/generate_uid', generate_uid);
module.exports = app;
So I really don't know what the solution is. Thanks in advance for your answers !!
As said in the comments, it looks like the app doesn't have the .listen() function, which is very important to bind to the port.
app.listen(3000, () => {
console.log("Server started in port 3000!");
});
I'm learning Js.
I have a problem in the code:
const express = require('express');
const app = express();
const PORT = 3000;
app.listen(PORT, function(){
console.log("The Express is running");
});
app.get('/', (req,res)=>{
res.send('Ok');
});
running node app.js i have the answer "Ok".But, running in the browser(localhost:3000) i get : "The connection was refused".
necessary info: I'm use a container docker, which I use the ubuntu. Can this cause a problem?
I need my server to go up, call a function then go down.
From what I've seen on the web I should use this section of code:
const express = require('express');
const app = express();
app.on('listening', () => {
console.log("Server up...");
console.log("Server going down...");
});
but for some reason this does not work for me.
The program does go up but the logs are not written.
const express = require("express");
const app = express();
const server = app.listen(3000);
//call whatever function you need here
server.close(() => console.log("Server going down..."));
First we start the server using app.listen and later when you want to close it just do server.close()
I am building a simple nodejs server to serve static content for a webpage. I want to send status codes back but when I try to create a function for the app.use() the content is not served.
var express = require('express')
var app = express();
app.listen(3000, function() {
console.log("We are now listening on Port:3000")
app.use(express.static('public'));
app.use('*', express.static('public/404.html'));
});
The content is served when I run this, but I cannot get the status codes as I need. Any help is appreciated.
I don't know exactly what you want. This is my suggest solution:
var express = require('express')
var app = express();
app.listen(3000, function() {
console.log("We are now listening on Port:3000")
app.use(express.static('public'));
app.use('*', function(req, res) {
// change your status code here
res.status(404).sendFile(__dirname + "/public/404.html");
});
});
I'm using the body-parser npm package to parse POST data in Application/JSON format and using a few different express routers to modularize the routes I'm creating. Here's the important info in my main server.js file:
server.js
var express = require('express');
var bodyParser = require('body-parser');
app.use(bodyParser.json());
// I'm using a sessionId to identify a person in this dummy app instead of adding in authentication. So the API call is sent to http://localhost:5000/johndoe/todo
app.use('/:sessionId/todo', require('./routes/todoRoutes'));
var port = process.env.PORT || 9191;
app.listen(port, function () {
console.log('Express server listening on port ' + port);
});
todoRoutes.js:
var todoRouter = require('express').Router();
todoRouter.post('/', function (req, res) {
console.log("This is the post data:");
console.log(req.body);
});
module.exports = todoRouter;
req.body seems to be getting lost through the middleware; it logs {} to the console. However, on a whim I added the following to my bodyParsing middleware in server.js:
app.use(bodyParser.json(), function (req, res, next) {
next();
});
And now it's passing req.body through to todoRoutes.js. (It now logs {title: 'Testing'}, etc. to the console like I need it to.)
What's going on here? And what's the best way to structure this so that it works the way it's supposed to? I'm new to Express, so I admit I could be structuring this all wrong.
Ugh, nevermind. I was an idiot and found that the Content-Type Header of application/JSON got turned off in Postman (read: I must have turned it off at some point) and so the bodyParser was never being used.
Dammit.
Thanks for the help to those who responded!