简体   繁体   中英

Trying to run node app on fedora server

Ok I am making website and want to use mongo, express, etc. I setup a server using fedora server ISO. The problem is getting node working. I have followed several tutorials, and its all the same. Nothing works. So I have to be doing something wrong. Trying to get the simplest thing to display on screen.

I think the server is running httpd server, whatever fedora has built in. I get the default fedora server page when going to the url. So the server is running and working, just hasn't been configured. When running node on the server do I have to use httpd-node? Or can it be http, etc.

Here is my app.js

const express = require('express')
const app = express()

app.get('/', function (req, res) {
  res.send('Hello World!')
})

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})

And then I have a basic index.html that should be rendered just saying test.

I ssh into the server and run node start, it runs and the console logs the message like it should. But if I go to the address 192.168.1.5, or the domain that points to the server, I get nothing, just a blank page.

If someone can help me get this working, I can actually get to work coding out the application. Any help would be appreciated.

res.send('Hello World!') - this is your problem! Why?

How you receive this answer on client side?

Solution: use res.render(..) - for rendering from server or use AJAX on client side for receive this text!

PS: render page and you don't see blank page anymore! Or use client-server conversation logic with your server through AJAX.

Try 192.168.1.5:3000

If I wrong: show your full project setup... Test your app with curl ( https://curl.haxx.se )! Check connection establishment, and show results here!

I think you make a confusion. When you build an Express application, you do not need another server at all.

When you start your app with:

app.listen(3000, function () {})

Express returns an http.Server object listening to port 3000.
When you navigate to your local adress on port 3000, you will see your "hello world" message.

It is possible that httpd service is already running in your Fedora environnement on default port 80 (the default port for http, the one your reach when you go to your local adress) but this is an Apache server and you do not need it to run your Nodejs app.

To build a Nodejs server, you can also use httpd-node package, but this is redundant as you're using Express framework.

If you need to serve a simple html file, a method I like for its simplicity is to use ejs template engine, something like this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM