error in code while running in nodejs - node.js

var express = require('express'),
app = express.createServer();
app.use(express.logger());
app.get('/', function(req, res){
res.send('Hello World');
});
app.listen();
console.log('Express server started on port %s', app.address().port);
I get the error "unexpected identifier"
what is wrong with the code?

I don't think there's a method createServer in express. This is from their docs:
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send('hello world');
});
app.listen(3000);
http://expressjs.com/en/starter/hello-world.html

express.createServer() has been deprecated. Please check this question
Nodejs / Express - Launching my app: express.createServer() is deprecated
on how to update the project to work with more recent versions of express.

You didn't declare port anywhere and also you didn't mention the port number also.When you are using app.address().portit does not hold any port value so that's why it returns undefined.Here is my code hope this helps for you.Just save this code with a name I saved it as server.js
var http=require('http');
var express = require('express');
var app = express();
app.set('port', process.env.PORT || 8000);
app.get('/', function(req, res){
res.send('Hello World');
});
var server = http.createServer(app);
server.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
OUTPUT IN TERMINAL:
C:\Users\nodejs\tasks>node server.js
Express server listening on port 8000
Open a browser and type localhost:8000 then
OUTPUT IN BROWSER:
Hello World

Related

Basic express hello world

I have the following code in node.js which is very basic express code but I can't get it to work. when I visit localhost/, it gives me text "Cannot GET /". It should display Hello world.
const express = require('express');
const app = express();
app.get('/', function (req, res) {
res.send('Hello World!')
});
do you open port?
The server must open the port.
app.js
var express = require('express');
var app = express();
app.get('/', function(req, res) {
res.send("Hello World");
});
app.listen(3000, function() {
console.log("server open");
});
If listen method called by app, Prepares to receive the client's response to the port passed as the first argument.
U can execute this code.
node app.js OR supervisor app.js OR forever start app.js ETC

I can't connect to my node express server on any other device on my network

Here is my server.js code:
var express = require("express");
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(3000, '0.0.0.0', function ()
console.log('Example app listening on port 3000!')
})
Really simple, I just want to see it working on my other devices before I continue.
It works fine obviously on my laptop I'm running it off, but nothing else. Any ideas?
Try removing the '0.0.0.0'
You also have a missing bracket.
Try This:
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
Read more:
https://nodejs.org/api/http.html#http_server_listen_path_callback
Try this maybe:
var http = require("http");
var express = require("express");
var service = express();
service.use(service.router);
service.get('/', function (req, res) {
res.send('Hello World!')
});
var server = http.createServer(service);
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
});

access application port : express

I am trying to build a simple chat application with node.js using Express.My node version is v0.12.7 and express version is 4.13.1.
I need to access the port the application is listening to but I am not getting how to do that even after lot of research over google.
My code in index.js is:
var express = require('express');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
io.on('connection', function(socket){
console.log('a user connected');
});
console.log(app.get('port')); // not working
http.listen(app.get('port'), function(){
console.info('Server listening on port :');
});
module.exports = router;
Please suggest If this can be done and how. Thanks in advance :)
In order to get the port, you're going to have to set it first like this
var app = require('express')();
app.set('port', process.env.PORT || 3000);
...

Express Node JS

Following is my code as follows:
var express = require('express');
var app = express();
var router = express.Router();
app.route('/')
.get(function(req, res) {
res.send('Hello World!');
});
app.route('/user')
.get(function (req, res) {
res.send('Hello' + req.params.id);
});
var server = app.listen(8000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
It runs fine with http://localhost:8000/ but with http://localhost:8000/user?id=D it gives following error: Cannot GET /user?id=D.
WHat is wrong in my code? Please help.
Thanks.
This route syntax:
'/user/:id'
matches a URL like this:
http://localhost:8000/user/4095
If you use a URL like this:
http://localhost:8000/user?id=D
then, you will need to use a "/user" route and read the query parameter for the id value from req.query.id as described here.
In addition, your don't need the app.route() as it's just an extra level of complication for things you are not doing here. I'd suggest this simplification which I have tested and works:
var express = require('express');
var app = express();
app.get('/', function(req, res) {
res.send('Hello World!');
});
app.get('/user', function (req, res) {
res.send('Hello: ' + req.query.id);
});
var server = app.listen(8000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});

Access the public folder when using node and Express?

I have looked through stackoverflow and read the express documentation, I can't figure out why the app won't run when I implement "app.use(express.static());" does anyone know a fix?
var express = require('express')();
var app = require('express')();
var server = require('http').Server(app);
var io = require("socket.io").listen(server);
//If i use this my app will not start
// app.use(express.static());
app.get('/', function(req, res){
res.sendfile('index.html');
});
//Get input from front-end
io.on('connection', function(socket){
// On input do something
socket.on('directional in', function(unique_id, input, input1){
// send info to index
io.emit('directional out', unique_id, input, input1);
});
});
server.listen(3000, function(){
// Server is running
console.log('listening on *:3000');
});
Any help would be great!
You're not initialising express correctly. The correct way would be as follows:
var express = require('express');
var app = express();
With this, you will be able to do
app.use(express.static(__dirname + '/public'));
All together, a fully functional express app would look like this in its most basic form:
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/public'));
app.listen(process.env.PORT || 3000);
Let me know what happens.

Resources