代码之家  ›  专栏  ›  技术社区  ›  AlwaysNeedingHelp

获取https express节点应用程序以在共享主机上工作

  •  1
  • AlwaysNeedingHelp  · 技术社区  · 7 年前

    所以我在这里不知所措,真的需要一些指导。

    我一直在关注微软关于edX的NodeJS简介课程。我们必须创造一个 使用express的简单restfulblogapi。我一直想让它在我的共享主机上运行。

    我所做的:

    • these instructions 使用node的最新稳定版本安装nvm。
    • 跟着 this question 让我的express应用程序使用https
    • 已将应用程序上载到宿主服务器。
    • SSH进入宿主服务器,运行 npm install .
    • 跑 node server.js 启动服务器。一开始似乎还不错。
    • 尝试在应用程序正在侦听的端口上导航到我的网站 https://samkeene.co.uk:3000/blog-api
    • 期待看到“你好世界”,但却得到了一个暂停。

    服务器.js

    const routes = require('./routes');
    const express = require('express');
    const logger = require('morgan');
    const bodyParser = require('body-parser');
    const errorhandler = require('errorhandler');
    const https = require('https');
    const fs = require('fs');
    const path = require('path');
    
    let store = {
        posts: []
    };
    
    let key = fs.readFileSync(path.join(__dirname,'../../ssl/keys/c07f7_003d1_ecb06ccb2afcd72cfa43b6011c82464e.key'));
    let cert = fs.readFileSync(path.join(__dirname,'../../ssl/certs/samkeene_co_uk_c07f7_003d1_1574035199_5b90fc5e96ac0c534d2ee116af6fd342.crt'));
    let options = {
        key: key,
        cert: cert
    };
    
    let app = express();
    app.use(bodyParser.json());
    app.use(logger('dev'));
    app.use(errorhandler());
    
    app.use((req, res, next) => {
        req.store = store;
        next();
    });
    
    app.get('/', (req, res) => {
        res.send('hello world')
    });
    
    app.get('/posts', routes.Posts.getPosts);
    app.post('/posts', routes.Posts.addPost);
    app.put('/posts/:postID', routes.Posts.updatePost);
    app.delete('/posts/:postID', routes.Posts.removePost);
    
    app.get('/posts/:postID/comments', routes.Comments.getComments);
    app.post('/posts/:postID/comments/', routes.Comments.addComment);
    app.put('/posts/:postID/comments/:commentID', routes.Comments.updateComment);
    app.delete('/posts/:postID/comments/:commentID', routes.Comments.removeComment);
    
    let server = https.createServer(options, app);
    
    server.listen(3000, () => {
        console.log("server starting on port : " + 3000)
    });
    

    git repo . (或导航到 https://www.samkeene.co.uk/blog-api/ )

    服务器.js

    const express = require('express');
    
    let app = express();
    
    app.get('/', (req, res) => {
        console.log("hello");
        res.send('hello world')
    });
    
    app.listen(3001);
    

    1 回复  |  直到 7 年前
        1
  •  0
  •   Rastalamm David Lung    7 年前

    对于http,在端口8080上侦听

        2
  •  0
  •   taiff    6 年前

    我通过cPanel和NodeJS版本9.11.2设置了一个NodeJS应用程序,成功地运行了以下程序:

    var express = require('express');
    var app = express();
    
    app.get('/', function(req, res) {
        res.send('Howdy! req.url: '+req.url);
    });
    app.get('/users', function(req, res) {
        res.send('Should send a bunch of users!');
    });
    app.get('/things/:name/:id', function(req, res) {
        res.send('id: ' + req.params.id + ' and name: ' + req.params.name);
    });
    app.listen();
    

    我不必关心端口号,也可以通过HTTPS访问这些端点。

    在您的情况下,只需删除3001,看看它是否与您的共享主机工作。

    希望有帮助。:)

    推荐文章