简体   繁体   中英

express js routes how to get the root

I'm making a website using angular and when the user hits '/' root i want to go the my www/index.html and render that page, and after that angular can take care of the rest. I just want my application to load that index.html before angular takes over.

my question is how do i make my app initialize, and then i can do the rest with angular.

this is my server.js

    // dependencies
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');

// mongodb
mongoose.connect('mongodb://localhost/test');

// express
var app = express();

//route for the root
app.get('/', function(req, res){

  res.render('www/index.html');

});

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

// routes
app.use('/api', require('./routes/api'));

// start server
app.listen(3000);


i have a routes/api.js file that basically takes care of the api calls

    // dependencies
var express = require('express');
var router = express.Router();
var app = express();

// models
var User = require('../models/user');

// routes
User.methods(['get', 'put', 'post', 'delete']);
User.register(router, '/users');

// return router
module.exports = router;

If you just want your index.html to be the entry point into your angular app I think you need to serve it as a static asset. Try using express.static and pass it the directory that contains your index.html file. Try something like..

app.use(express.static('./public/'));
app.use('/*', express.static('./public/index.html'));

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