Express not loading in URL - node.js

I currently have an express server I'm running based on the express tutorial.
Here is my code for server.js
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
the server.js file is in a directory on my server:
http://myurl.com/node-webshot/server.js
I start the server with: npm start
and get this:
> webshot#0.18.0 start /home/myurl/node-webshot
> node server.js
Example app listening on port 3000!
when I go to my url: http://myserver.com/node-webshot/
I see the directory listing only:
note "myserver.com" is just a fill in to path for working folder.
My expected result would be that when I go to the roof of the file where server.js is loaded that it would show the "Hello world". Not sure what I'm missing here.

Related

How to serve a specific file node server

Let us say I have a directory with 2 files: c:\file1\index.html and c:\file1\test.html. I am trying to start a server with node js on a specific port. Now I used app.use('/', express.static(path.join(__dirname))) as the directory to serve the file. My question is how can I make it serve c:\\file1\test.html with the code I have right now it serves c:\\file1\index.html? Thanks in advance.
const express = require('express')
const app = express()
const path = require('path')
//loads index.html
app.use('/', express.static(path.join(__dirname)))
app.listen(3000, () => console.log('Server up on port 3000'))
Would this work for you?
const express = require('express')
const app = express()
//specify public folder
app.use(express.static('file1'));
app.listen(3000, () => console.log('Server up on port 3000'));

I want to check the server.When I tried to run the code in console using:Node server.js, it didn't run the code. Any ideas why?

I want to check the server.When I tried to run the code in console using:Node server.js, it didn't run the code. Any ideas why?
This is my code where I tried to check the server
const express = require("express");
const app = express();
app.get("/", function(request, response){
console.log(request);
})
app.listen(3000, function () {
console.log("Server started on port 3000");
});
and here I try to run the code in console, and it simply doesn't do anything: it shows me the location and that's all
Helens-MacBook-Pro:my-express-server helenmyrlen$ node server.js
Helens-MacBook-Pro:my-express-server helenmyrlen$
I am also new to NodeJS so I can't find where you are doing are but you can try with the below script. It works.
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
Run the app with the following command:
$ node app.js
Then, load http://localhost:3000/ in a browser to see the output.

Confused about Express, node.js terminology

I am new to web development. I am currently learning express.js. The following chunk of code and text is from their documentation.
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))
This app starts a server and listens on port 3000 for connections.
I am confused as to what the server is here. Which line of code refers to the 'creation of the server'? Is the express app the server itself, or is it only listening for requests on port 3000, while the server is something else?
Thanks a lot!
Basically Express is a framework of Node Js, Like Python has Django, Java has Spring etc..
When you create server in node js you use HTTP module, In express by inside function they provide listen function.
When you create Server using Node you use below code
http.createServer(function (req, res) {
res.write('Hello World!');
res.end(); //end the response
}).listen(8080);
So in node http module have Listen function & in express js express module have listen function.
app.listen creates a new server. In express there is no any terminology of CreateServer. So express is very much flexible to use.
Please follow this url http://expressjs.com/en/guide/writing-middleware.html
At the minute you call listen the server is going to start running, listening to the PORT you have defined.
Here is a line by line commented version of your code :
//We are creating the express app by setting it to the app variable.
const express = require('express')
//The express object
const app = express()
//The port
const port = 3000
/*
.get is telling to the express object that when it gets that route ('/')
it should give the specified response : 'Hello World!' for our case.
It takes in 2 arguments:
(1) the url - the route
(2) the function that tells express what to send back as a response for the
request - the callback function
*/
app.get('/', (req, res) => res.send('Hello World!'))
//.listen is going to bind the application to the port 3000.
app.listen(port, () => console.log(`My awesome app is listening at
http://localhost:${port}`))
To find out about the difference between the concepts node and express, I found this response usefull.
As you said, that entire chunk "create" the server, its not only one line to "create" the server.
Using node and npm you install express const express = require('express')
In this line yo use express framework const app = express()
In this line you set a port const port = 3000
In this line you create the main root app.get('/', (req, res) => res.send('Hello World!'))
and this line use the port and up runnig your web server app.listen(port, () => console.log(Example app listening at http://localhost:${port}))
As you can see, all of then combined "creates" the server

how to generate the localhost URL by executing a node js file?

I wrote a program of node js on Brackets Text Editor and saved it with name first.js. When I am executing it with command prompt using -
node first.js
var http = require('http');
function onRequest(req,res)
{
res.writeHead(200,{'Content-Type':'text/plain'});
res.write('hello js');
res.end();
}
http.createServer(onRequest).listen(8080);
It is running fine but when I am trying to run it on the localhost, it is not working.
The program that I wrote was -
If you did try localhost:8080 and that still doesn't work, it could be that some other program is running on that port. Try .listen(3000) then visit localhost:3000.
you can create a simple http server using express (be sure that u have the packaged installed , you can install express using npm : npm install express) .
your server.js code is :
const express = require("express");
const app = express();
const port = process.env.PORT || 4000;
app.get("/", (req, res) => {
console.log("response");
});
app.listen(port, () => {
console.log("Server listining on port ", port);
});
lets say you save this file in c:/folder , open cmd in the same folder
run : node server.js .
now go to your browser and check : http://localhost:4000/

Express launching Angular application

I have an express server setup online which loads multiple ports and those ports are setup on subdomains for example. port 9000 loads the main domain.com port 8000 loads the main application at "app.domain.com" port 1000 loads "signup.domain.com" and the build version of the app is on port 8500 "build.domain.com".
The application is an Angular application however when I go to load the Angular app it loads on port 4200 or it says 8500 is in use. So currently I am loading that in express like so:
// Build Application - In Development
var appbuild = express();
appbuild.get('/', function (req, res){
res.sendFile('/app/build/myapp/src/index.html', { root: '.' })
});
var port = 8500;
appbuild.listen(port);
console.log('Build App Listening on port', port);
So my question is in Express how can I instead of writing sendfile command make it launch the angular app in that location on port 8500 so my subdomain names will work. The reason I'm asking this is because right now all it does is load the index file but angular or the app isn't running so i just see source code that says app-root and a blank white page.
Thank you in advance.
Robert
--- Update. I've decided to post the entire Express file. My issue is trying to load a angular app on port 8500 from the subfolder upon booting of express. Here is the full server.js code:
// server.js
const express = require('express'),
path = require('path'),
bodyParser = require('body-parser'),
cors = require('cors'),
mongoose = require('mongoose'),
config = require('../config/DB');
mongoose.Promise = global.Promise;
mongoose.connect(config.DB).then(
() => {console.log('Database is connected') },
err => { console.log('Can not connect to the database'+ err)}
);
// Main Website
var web = express();
web.get('/', function (req, res){
res.sendFile('/web/index.html', { root: '.' })
});
var port = 9000;
web.listen(port);
console.log('Web Listening on port', port);
// Main Application
var app = express();
app.get('/', function (req, res){
res.sendFile('/app/index.html', { root: '.' })
});
var port = 8000;
app.listen(port);
console.log('Main App Listening on port', port);
// Build Application - In Development
var appbuild = express();
appbuild.get('/', function (req, res){
res.sendFile('/app/build/myapp/src/index.html', { root: '.' })
});
var port = 8500;
appbuild.listen(port);
console.log('Build App Listening on port', port);
// Sign up Portal
var sign = express();
sign.get('/', function (req, res){
res.sendFile('/signup/index.html', { root: '.' })
});
var port = 10000;
sign.listen(port);
console.log('Sign Up Portal Listening on port', port);
Refer to this link https://malcoded.com/posts/angular-backend-express
Update your code to the following:
const express = require('express');
const app = express();
app.listen(8500, () => {
console.log('Server started!');
});
You need to build the angular app if your angular version not 1.x
ng build
Also, I think this question is similar to your question:
Not able to view Angular app via express/heroku?

Resources