【问题标题】:How to combine node,express,mongodb with angularjs?如何将node、express、mongodb与angularjs结合起来?
【发布时间】:2018-03-12 12:30:37
【问题描述】:

我已经使用 node、express、mongodb 创建了应用程序(在 Node、Express 和 MongoDB 中构建 RESTful API)现在我想在下面添加角度前端是我的server.js。我可以在这个文件中添加什么。我想为此应用程序构建前端(angularjs)端。现在如果我运行 node server.js 仅在 808l 端口(localhost:8080)中运行的节点服务器我想在同一个端口中运行 angular 和 node 我如何实现这一点帮助我出来了吗?

server.js

// BASE SETUP
// =============================================================================

// call the packages we need
var express    = require('express');
var bodyParser = require('body-parser');
var app        = express();

// configure app
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var port     = process.env.PORT || 8080; // set our port

var mongoose   = require('mongoose');
mongoose.connect('mongodb://localhost//:20717'); // connect to our database
var Students     = require('./app/models/students');

// ROUTES FOR OUR API
// =============================================================================

// create our router
var router = express.Router();

// middleware to use for all requests
router.use(function(req, res, next) {
    // do logging
    console.log('Something is happening.');
    next();
});

// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', function(req, res) {
    res.json({ message: 'hooray! welcome to our api!' });   
});


router.route('/students')

    // create a students (accessed at POST http://localhost:8080/students)
    .post(function(req, res) {

        var students = new Students();      // create a new instance of the Students model
        students.name = req.body.name;  // set the students name (comes from the request)

        students.save(function(err) {
            if (err)
                res.send(err);

            res.json({ message: 'Students created!' });
        });


    })

    // get all the students (accessed at GET http://localhost:8080/api/students)
    .get(function(req, res) {
        Students.find(function(err, students) {
            if (err)
                res.send(err);

            res.json(students);
        });
    });



// REGISTER OUR ROUTES -------------------------------
app.use('/api', router);

// START THE SERVER
// =============================================================================
app.listen(port);
console.log('Magic happens on port ' + port);

下面是我的文件夹结构

【问题讨论】:

  • AngularJS 不在任何端口上运行。它不是网络服务器。它只是一个 Javascript 应用程序,仅在 Web 浏览器中运行。需要指示 Node/express 提供静态资源,例如 index.html、作为 javascript 文件的 Angular 应用程序以及普通网站应该具有的任何样式、图像、字体等

标签: javascript angularjs node.js mongodb


【解决方案1】:

首先,您需要安装一个名为path 的软件包。然后,您可以将 Angular 项目的 index.html 文件作为 server.js 放在嵌套目录中。让我们把它放在一个名为public 的目录中,所以在你的server.js 中,添加这行代码:

const path = require('path');
app.use(express.static(path.join(__dirname, 'public')));

所以您可以直接从您的网络浏览器访问它,只需访问 localhost:8080(您的服务器 URI)

【讨论】:

  • 好的,所以您可以在您的appnode_modules 文件夹旁边添加您的angular-project-folder。并像我在回答中提到的那样使用它
  • 你能看到我的问题我在那里更新了文件夹结构图像(节点、快递、mongodb、angular.html)
  • 嘿,我添加了这两行和公用文件夹。现在我想知道我应该在'index.html'中做什么
  • 您的index.html 应该是您的角度应用程序的入口点。换句话说,您需要将您的 anguar 应用程序放在 public 文件夹中。然后你可以通过你的网络浏览器访问你的服务器 URI (localhost:8080)
  • 我怎样才能访问 api ?localhost:8080/apiname 这将工作??
猜你喜欢
  • 2016-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-08
  • 2012-03-17
  • 2017-12-15
  • 1970-01-01
相关资源
最近更新 更多